try:
# Some Code....
except:
# Handling of exception
return False
else:
# execute if no exception
finally:
print('done')
# Execute before returning False incase of an exception
How do I get the code in finally to execute before returning False in except ?
This should already work. A return, break or continue acts the same as an exception for a finally block - it runs, then execution continues with those.
Note that the docs appear to imply this is only the case for the try block, though it clearly also applies for the except block:
When a return, break or continue statement is executed in the try suite of a tryβ¦finally statement, the finally clause is also executed βon the way out.β