+ 4

The finally statement

Hello everyone. I have a question about the finally statement which comes after the try and except statements: what is the difference between the code putted in finally statement and the same code without finally statement and without indentation? Here is an example: ######### with finally statement try: print("Hello") print(1 / 0) except ZeroDivisionError: print("Divided by zero") finally: print("This code will run no matter what") ######### without finally statement: try: print("Hello") print(1 / 0) except ZeroDivisionError: print("Divided by zero") print("This code will run no matter what")

23rd Aug 2019, 11:37 AM
M'barek IAOUSSE
M'barek IAOUSSE - avatar
16 Réponses
+ 5
If except throws an error, finally will still get executed; but other code won't be
23rd Aug 2019, 11:41 AM
Airree
Airree - avatar
+ 5
Try this code, there is a difference: try: print("Hello") print(a) except ZeroDivisionError: print("Divided by zero") finally: print("This code will run no matter what") ######### without finally statement: try: print("Hello") print(a) except ZeroDivisionError: print("Divided by zero") print("This code will run no matter what")
23rd Aug 2019, 1:37 PM
Paul
Paul - avatar
+ 3
Finally IS FINAL STATEMENT Always 🏜️
24th Aug 2019, 9:03 AM
Sanjay Kamath
Sanjay Kamath - avatar
+ 2
Airree I have edited the question with an example, both codes give the same thing. What do you think the difference is??
23rd Aug 2019, 1:00 PM
M'barek IAOUSSE
M'barek IAOUSSE - avatar
+ 2
If there's no difference, why use finally anawy. I think there's something but we didn't get it concretely. Airree
23rd Aug 2019, 1:07 PM
M'barek IAOUSSE
M'barek IAOUSSE - avatar
+ 2
Airree I don't know whether you're talking about my sentences or the code itself. My question is: what is the usefulness of finally statement?? I gave the example from the python course. Thank you
23rd Aug 2019, 1:11 PM
M'barek IAOUSSE
M'barek IAOUSSE - avatar
+ 2
Thank you anyway. The explanation you gave is what is written in the course but. I think we need someone who has deep knowledge to clarify it better for us.
23rd Aug 2019, 1:15 PM
M'barek IAOUSSE
M'barek IAOUSSE - avatar
+ 2
But if you got the right answer from the course why did you need to ask it?
23rd Aug 2019, 1:17 PM
Airree
Airree - avatar
+ 2
Because we should ask more to understand more. As you can see both examples I gave in the question work the same (with and without finally statement). So in order to get a clear idea about exceptions (as an example) we should ask and communicate.
23rd Aug 2019, 1:20 PM
M'barek IAOUSSE
M'barek IAOUSSE - avatar
+ 2
Paul Jacobs thank you very much. I think I get the idea now.
23rd Aug 2019, 2:21 PM
M'barek IAOUSSE
M'barek IAOUSSE - avatar
+ 2
The rationale for using "finally". Your program might do some things that somehow need to be taken care of, even when an error occurs. You might open a database connection or create temporary files in the beginning, then an error occurs within the try/except block. If you use 'finally' to clean up those pending issues (close the connection, delete the temp file) your program is safer to use even when an unexpected error occurs, you don't leave garbage behind and free up memory. Sometimes it is more convenient to use context managers ('with' keyword) to open files and databases, because they automatically take care of this cleanup. https://www.geeksforgeeks.org/context-manager-in-JUMP_LINK__&&__python__&&__JUMP_LINK/
24th Aug 2019, 7:33 AM
Tibor Santa
Tibor Santa - avatar
+ 2
The finally block still gets executed when before the program crashes if there's an error in the catch block
4th Sep 2019, 7:28 AM
Manuel Schwartz
+ 1
There is no difference.
23rd Aug 2019, 1:04 PM
Airree
Airree - avatar
+ 1
These sentences literally don't make any sense.
23rd Aug 2019, 1:08 PM
Airree
Airree - avatar
+ 1
I already explained it. Thank you
23rd Aug 2019, 1:12 PM
Airree
Airree - avatar
+ 1
'try:' part will always run first, and then you have two situation: 1. If "try" part executed correctly, "except:" part will be ignored 2. If any error in "try:" part, it will still run until error happened. when error happened, the rest of "try:" part will be ignored, and then jump into "except:" part immediately. No matter what happened(at both situations), the "finally" part always run. (even some error also happened in 'except' part...) The 'finally' part runs looks like the key word 'finally' just doesn't exist, but still some different. If try/except crashed, the rest code won's execute, but "finally" will still execute before the code crashed. ^ω^
25th Aug 2019, 10:34 AM
mounir abdelmolk
mounir abdelmolk - avatar