What's the difference between using 'finally' in my Python vs moving the "final" code out of the 'except' block? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

What's the difference between using 'finally' in my Python vs moving the "final" code out of the 'except' block?

try: print("may cause error") except: print("error-handling statement") finally: print("I always want this code to run at the end") 3rd print statement executes after if we take out 'finally'. Is finally just to make clean code?

20th Mar 2019, 12:18 PM
Zoee
2 Answers
+ 6
Finally is always executed at the end of a try/except block regardless the outcome of the block
20th Mar 2019, 12:27 PM
Nathan Lewis
Nathan Lewis - avatar
+ 5
Zoee it is not always that the code after the try/except block will run. But if you use the code in finally it will always run.
20th Mar 2019, 12:28 PM
Ulisses Cruz
Ulisses Cruz - avatar