Exception Groups (3.11+)
PY · Error Handlingsyntax
raise ExceptionGroup(msg, [exc1, exc2])
except* ErrorType as eg:example
def validate(data: dict) -> None:
errors = []
if not data.get("name"):
errors.append(ValueError("name is required"))
if not data.get("email"):
errors.append(ValueError("email is required"))
if errors:
raise ExceptionGroup("Validation failed", errors)
try:
validate({})
except* ValueError as eg:
for err in eg.exceptions:
print(f" - {err}")output
- name is required
- email is requiredNote except* catches matching exceptions from the group while letting others propagate. ExceptionGroup is ideal for concurrent/batch operations that produce multiple errors.