except Except handler
Last updated: 2026-09-22
except Except handler
except handles a matching exception (or tuple of types); as binds the exception object.
| Category | Keywords & Statements |
|---|---|
| Kind | Keyword / Statement |
| Python Version | all |
📝 Syntax
try:
block
except ExceptionType [as name]:
handler
▶ Example
Edit the code, press Run, and see the result in the output panel:
Output:
div by zero
🏷️ Related Keywords / Statements
💡 Related Cases
More case studies coming soon — check back later.
❓ FAQ
QCan a single `except` catch several exception types?
AYes — write them as a tuple: `except (ValueError, TypeError) as e`. Note that subclass exceptions must come before their parent class, otherwise the parent branch catches them first.
QI caught an exception but the program still crashed. Why?
AMost likely the exception type did not match; for example the code actually raised `TypeError` but you wrote `except ValueError`. Also, `except Exception` does not catch `KeyboardInterrupt` and `SystemExit`, which derive from `BaseException`.
QWhat is `e` in `except ... as e`?
A`e` is the exception object; `str(e)` gives you the error message. In Python 3, the reference named `e` is automatically deleted when the `except` block ends, so it is not held for a long time and cause problems.