Errors
Exception Groups
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
2except* 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 modeNotes
except*is forExceptionGroup, not ordinary single exceptions.- Each
except*clause handles matching members of the group. - Exception groups often appear around concurrent work.
See also
- alternative: Exceptions
- related: Exception Chaining
- next depth: Async Await
Run the complete example
Expected output
2
ExceptionGroup
bad port
bad mode
Execution time appears here after you run the example.