Errors

Exception Groups

except* handles matching exceptions inside an ExceptionGroup.

An exception group bundles several exception objects. This is different from an ordinary exception because more than one failure is present.

Source

errors = ExceptionGroup(
    "batch failed",
    [ValueError("bad port"), TypeError("bad mode")],
)
print(len(errors.exceptions))

Output

2
BEFOREexcept*AFTER
except* peels matched leaves out of an ExceptionGroup; survivors regroup and propagate.

except* handles matching members of the group. The ValueError handler sees the value error, and the TypeError handler sees the type error.

Source

try:
    raise errors
except* ValueError as group:
    print(type(group).__name__)
    print(group.exceptions[0])
except* TypeError as group:
    print(group.exceptions[0])

Output

ExceptionGroup
bad port
bad mode

Notes

See also

Run the complete example

Example code

Expected output

2
ExceptionGroup
bad port
bad mode

Execution time appears here after you run the example.