+ 1
What is the point of using "finally"
try: print("Hello") print(1 / 0) except ZeroDivisionError: print("Divided by zero") finally: print("This code will run no matter what") Instead of using the keyword "finally", can't we just remove the indent for the last print statement like this: try: print("Hello") print(1 / 0) except ZeroDivisionError: print("Divided by zero") print("This code will run no matter what") the above 2 codes give the exact same output, so what exactly is the point of using "finally"? maybe it makes the code a bit more readable, but is there anything else I should know about the keyword "finally"?
3 Réponses
+ 3
finally block is used to deallocate the system resources. ... The finally block is always executed, so it is generally used for doing the concluding tasks like closing file resources or closing database connection or may be ending the program execution with a delightful message.
+ 2
There are some good answers I found on this thread
https://stackoverflow.com/questions/11551996/why-do-we-need-the-finally-clause-in-JUMP_LINK__&&__python__&&__JUMP_LINK
+ 1
thanks abhayand Indranil
I found more convincing answer on the stackoverflow thanks