+ 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"?

12th Oct 2020, 11:16 AM
Hosea
3 ответов
+ 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.
12th Oct 2020, 11:34 AM
Indranil
Indranil - avatar
12th Oct 2020, 11:47 AM
Abhay
Abhay - avatar
+ 1
thanks abhayand Indranil I found more convincing answer on the stackoverflow thanks
12th Oct 2020, 12:18 PM
Hosea