finally in Python Exception Handling | Sololearn: Learn to code for FREE!
Новый курс! Каждый программист должен знать генеративный ИИ!
Попробуйте бесплатный урок
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?

13th Jun 2019, 1:55 AM
harshit
harshit - avatar
1 ответ
+ 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.
13th Jun 2019, 3:45 AM
Frost
Frost - avatar