0
finally in Python Exception Handling
If exception occurs in a statement then why the statement we have mentioned in finally gets executed first and then the exception/error message?
1 Answer
+ 10
From python docs:
A finally clause is always executed before leaving the try statement, whether an exception has occurred or not. When an exception has occurred in the try clause and has not been handled by an except clause (or it has occurred in a except or else clause), it is re-raised after the finally clause has been executed. The finally clause is also executed âon the way outâ when any other clause of the try statement is left via a break, continue or return statement.
Hence, the finally block is executed before the error message is shown.