finally statement. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

finally statement.

when finally function work? if there is no error in mathematical operation then finally statement execute or not? try: math=4/4 print(math) except: print("an error occured") finally: print("this code will run no matter what") output of this code is 4 this code will run no matter what tell me why it so?

15th Jan 2019, 5:07 AM
fenil
fenil - avatar
3 Answers
+ 5
Are you sure it outputs 4? It should be one. The try, except, finally block is explained here https://www.sololearn.com/learn/JUMP_LINK__&&__Python__&&__JUMP_LINK/2441/ https://www.sololearn.com/learn/Python/2442/ If in the try block an error occurs, the except block is called. The finally block is always called, no matter if there was an exception or not
15th Jan 2019, 5:19 AM
Martin Möhle
Martin Möhle - avatar
+ 3
Yes the finally section will be executed in the end, regardless if there was an error or not.
15th Jan 2019, 5:19 AM
Tibor Santa
Tibor Santa - avatar
+ 1
the output is 1 insted of 4. it is my mistake
16th Jan 2019, 12:49 PM
fenil
fenil - avatar