Python finally & else
In this page:
try:
# risky code
except ExceptionType:
# handle error
else:
# runs if no error
finally:
# always runs
The else Block
An else block attached to try/except runs only when the try block completed without raising any exception -- this keeps code that should run 'on success' clearly separated from the risky code being guarded, rather than tacking it onto the end of the try block itself.
Example: The else Block
try:
result = 10 / 2
except ZeroDivisionError:
print("Error")
else: # runs only because the try block succeeded
print("Success:", result)
The else Block Real Example
A typical use is opening a file in the try block and reading from it in the else block -- if opening fails, you don't want to accidentally attempt the read too, and separating them into try/else makes that guarantee explicit rather than implicit.
Example: The else Block Real Example
try:
f = open("data.txt", "w")
f.write("saved")
except OSError:
print("Could not open file")
else: # only reached if opening and writing above succeeded
f.close()
print("File written and closed")
The finally Block
The finally block runs no matter what happens -- whether the try succeeded, an exception was caught, or even if the exception wasn't caught at all -- which is why it's the standard place to release resources like closing a database connection or a file handle.
Example: The finally Block
try:
print(10 / 2)
finally: # runs whether or not an exception occurred
print("This always runs")
Complete try-except-else-finally Flow
Combining try, except, else, and finally in one structure gives you four distinct phases: risky code, error handling, success-only follow-up code, and guaranteed cleanup -- each with a clear, separate responsibility instead of tangled conditional logic.
Example: Complete try-except-else-finally Flow
try:
value = int("42")
except ValueError:
print("Conversion failed")
else:
print("Converted:", value) # runs only on success
finally:
print("Done") # runs regardless
Return Values and finally
Even if a return statement executes inside the try or else block, Python still runs the finally block before the function actually returns to its caller -- this guarantees your cleanup code executes even when the function is exiting early via return.
Example: Return Values and finally
def safe_divide(a, b):
try:
return a / b
finally:
print("Cleanup runs even though we returned") # finally still runs before returning
print(safe_divide(10, 2))
- Assuming
elseruns afterexcept, when it runs only if thetryblock raised no exception. - Putting a
returninsidefinally, which overrides the value returned bytryand can swallow exceptions. - Forgetting that
finallyruns even when the function returns early, so cleanup code there may run when you did not expect it.
Chapter Quiz — Complete all 5 topics to unlock
0/5 topics done
Complete these topics first: