Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Python

Keyword except

Handle the first except clause matching the raised exception class or a superclass.

Version: Python 3.14; repository image, production runtime not attested

Syntax

except (ValueError, TypeError) as error:
    ...

Handle the first except clause matching the raised exception class or a superclass.

A tuple groups alternatives and an as target is cleared at the end of the handler to break reference cycles. except Exception does not catch SystemExit or KeyboardInterrupt. Python 3.14 permits multiple unparenthesized types when as is absent. except* partitions an exception group into matching subgroups.

Plain except and except* cannot occur on the same try.

Examples

try:
    int("blue")
except (ValueError, TypeError) as err:
    print(type(err).__name__)
print("err" in globals())
try:
    raise TypeError("units")
except ValueError, TypeError:
    print("group")
try:
    raise ExceptionGroup("two", [ValueError(), TypeError()])
except* ValueError as part:
    print("value", len(part.exceptions))
except* TypeError as part:
    print("type", len(part.exceptions))

Result

ValueError
False
group
value 1
type 1

Plain `except` and `except*` cannot occur on the same try.

Sources