else and finally in Exception Handling
flowchart TD
A["try body"] --> B{"did it raise?"}
B -->|no| C["else"]
B -->|"yes, a matching type"| D["except"]
B -->|"yes, unmatched"| E["propagates on"]
C --> F["finally"]
D --> F
E --> F
F --> G["and only then does the exception continue, or the function return"]
H["a return in the try body"] --> F
I["else is NOT the same as putting it at the end of try"] --> J["code there is no longer guarded by except"]
else in try/except
Section titled “else in try/except”else runs only if no exception occurred.
try:
x = int("42")
except ValueError:
print("Invalid number")
else:
print("Parsed successfully:", x)finally in try/except
Section titled “finally in try/except”finally runs always (success or error). This is useful for cleanup.
f = None
try:
f = open("data.txt", "r")
print(f.read())
except FileNotFoundError:
print("File not found")
finally:
if f is not None:
f.close()
print("File closed")Prefer context managers when possible
Section titled “Prefer context managers when possible”Instead of manual .close(), use with.
try:
with open("data.txt", "r") as f:
print(f.read())
except FileNotFoundError:
print("File not found")When is finally important?
Section titled “When is finally important?”- closing files
- releasing locks
- closing DB connections
- cleaning up temporary files
Check yourself
Section titled “Check yourself”-
A function raises inside `try` and has `return 'done'` inside `finally`. What does the caller see?
Verified: the ValueError vanished entirely, with no traceback and nothing logged. Python 3.14 now warns at compile time — SyntaxWarning: 'return' in a 'finally' block.
pch.quizShowAnswer
B — `'done'` — the exception is discarded — Verified: the ValueError vanished entirely, with no traceback and nothing logged. Python 3.14 now warns at compile time — SyntaxWarning: 'return' in a 'finally' block.
-
When does the `else` clause of a `try` statement run?
`else` is the no-exception path. Putting that code at the end of `try` instead would leave it guarded by the `except`, so an unrelated error in it would be caught by a handler meant for the risky call.
pch.quizShowAnswer
C — Only when the `try` body did NOT raise — `else` is the no-exception path. Putting that code at the end of `try` instead would leave it guarded by the `except`, so an unrelated error in it would be caught by a handler meant for the risky call.
-
With no exception raised, which blocks run and in what order?
Verified. With an exception it is `try`, `except`, `finally` instead — `finally` runs on every path.
pch.quizShowAnswer
B — `try`, `else`, `finally` — Verified. With an exception it is `try`, `except`, `finally` instead — `finally` runs on every path.
-
What should `finally` be used for?
Releasing a lock, closing a handle, restoring state. Changing what the function returns from there is what silently swallows exceptions.
pch.quizShowAnswer
B — Cleanup that must happen on every path — Releasing a lock, closing a handle, restoring state. Changing what the function returns from there is what silently swallows exceptions.
🧪 Try It Yourself
Section titled “🧪 Try It Yourself”Exercise 1 – The else Clause
Section titled “Exercise 1 – The else Clause”Exercise 2 – The finally Clause
Section titled “Exercise 2 – The finally Clause”Exercise 3 – else + finally Together
Section titled “Exercise 3 – else + finally Together”pch.coffeeTagline
pch.coffeeCtapch.feedbackHeading
pch.feedbackSubheading