What is the purpose of the finally block in a try-except statement?

91 views

What is the purpose of the finally block in a try-except statement?

What is the purpose of the finally block in a try-except statement?

solveurit24@gmail.com Changed status to publish February 13, 2025
0

It executes regardless of whether an exception was raised or caught.

try:
    x = 1 / 0
except ZeroDivisionError:
    print("Error occurred.")
finally:
    print("This will always execute.")  # Output: This will always execute.

solveurit24@gmail.com Changed status to publish February 13, 2025
0