+ 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")
16 Réponses
+ 5
If except throws an error, finally will still get executed; but other code won't be
+ 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")
+ 3
Finally IS FINAL STATEMENT Always 🏜️
+ 2
Airree I have edited the question with an example, both codes give the same thing. What do you think
the difference is??
+ 2
If there's no difference, why use finally anawy. I think there's something but we didn't get it concretely. Airree
+ 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.
+ 2
But if you got the right answer from the course why did you need to ask it?
+ 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.
+ 2
Paul Jacobs thank you very much.
I think I get the idea now.
+ 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/
+ 2
The finally block still gets executed when before the program crashes if there's an error in the catch block
+ 1
There is no difference.
+ 1
These sentences literally don't make any sense.
+ 1
I already explained it. Thank you
+ 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.
^ω^