← Back to Python Course | Chapter 10: Exception Handling | Lesson 3 of 5

Python finally & else

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

python
try:
    result = 10 / 2
except ZeroDivisionError:
    print("Error")
else:
    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

python
try:
    f = open("data.txt", "w")
    f.write("saved")
except OSError:
    print("Could not open file")
else:
    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

python
try:
    print(10 / 2)
finally:
    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

python
try:
    value = int("42")
except ValueError:
    print("Conversion failed")
else:
    print("Converted:", value)
finally:
    print("Done")

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

python
def safe_divide(a, b):
    try:
        return a / b
    finally:
        print("Cleanup runs even though we returned")

print(safe_divide(10, 2))
🔒

Chapter Quiz — Complete all 5 topics to unlock

0/5 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.