Skip to main content

← All terms · Errors & control flow

try / except / finally

Handle exceptions. `finally` always runs (cleanup). Catching `Exception` is usually too broad — be specific.

Learn this interactively:

Open lesson lesson-101

Related — Errors & control flow

ExceptionAn object that signals something went wrong. Raised with `raise`, caught with `tContext managerAn object usable with `with`. Guarantees cleanup even on exception — `open()`, lEAFP“Easier to Ask Forgiveness than Permission”. Idiomatic Python: try the operation