Skip to the workspace
Pebellslearn by building

Python syntax & reference

Find the form you need, compare how it behaves, and open the complete explanation when you need more depth.

Python 3.10+ syntax; individual entries identify newer features. Browser exercises use the configured Python runner.

1 matching syntax entry

Handling expected failures

Learn in depth →

try/except handles selected exceptions; finally runs cleanup when control leaves the try statement.

try: ...
except ValueError as error: ...
else: ...
finally: ...

Example

try:
    number = int("bad")
except ValueError:
    print("invalid number")
finally:
    print("finished")

Result

invalid number
finished

Watch for: Catch the narrow failures you can handle. Returning from finally can hide a pending exception.

Practise this topic →
Complete explanation index (91 lessons indexed)

Exceptions & Errors

Technical sources and review date

Original Pebells examples. Reviewed 2026-10-28.

Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.