I have somthing similar in my code below, where I have multiple except statements and all of them have to execute the someCleanUpCode() function. I was wondering if there is a shorter way to do it. Like a block that is only being executed when there was an exception. I can't use the finally block because that's also being executed when the try doesn't raise an error. And I only need to execute the someCleanUpCode() when there was an error. I first want to print the error and after that, run the someCleanUpCode()
try:
dangerousCode()
except CalledProcessError:
print "There was an error without a message"
someCleanUpCode()
except Exception as e:
print "There was an error: " +repr(e)
someCleanUpCode()
someCleanUpCodeis called in every error case.