try / except

Catching errors so the program can react instead of crashing.

Also called: exception, raise, finally, traceback

Domain
Languages
Source
General
Language
Python

In plain English

Catch the specific error you expect, not every error.

Example

Rows with broken JSON are caught by try/except and skipped with a warning.

In code

try:
    data = json.loads(raw)
except json.JSONDecodeError as e:
    log.warning("Bad JSON: %s", e)
    data = {}
finally:
    f.close()

Related terms

Added 2026-11-30 · Last reviewed 2026-11-30