Keywords and Control Flow
except — Exception handlers
Handle exceptions that match an exception class or tuple of classes, including ordinary subclasses. Ordinary handlers are tried in order; at most one runs for an exception. A bare handler must be last and catches BaseException subclasses, so prefer specific exception types. The as target is cleared after the handler. In Python 3.11+, except* partitions exception groups by matching contained exceptions; multiple handlers can execute and unhandled parts propagate. Ordinary except and except* cannot appear in the same try statement.
Formal Syntax & Structure
python syntaxpython
try: suiteexcept ErrorType as error: suite
# Exception-group handling (3.11+)try: suiteexcept* ErrorType as group: suiteWorking Example
Example Snippetpython
try: raise ExceptionGroup("batch", [ValueError("price"), TypeError("quantity")])except* ValueError as group: print("value:", len(group.exceptions))except* TypeError as group: print("type:", len(group.exceptions))