AnDocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
ExceptionGroup (introduced in Python 3.11 via PEP 654) is a built-in exception type that allows multiple, independent exceptions to be raised and handled simultaneously. It acts as a container that aggregates a sequence of exception instances into a single tree-like structure, enabling the propagation of concurrent errors without suppressing any individual exception.
Class Hierarchy
The feature introduces two new built-in classes:BaseExceptionGroup: Inherits fromBaseException. It is instantiated if any exception within the group inherits fromBaseExceptionbut notException(e.g.,KeyboardInterruptorSystemExit).ExceptionGroup: Inherits fromBaseExceptionGroupandException. It is instantiated only when all contained exceptions are subclasses ofException.
Instantiation and Raising
An exception group is initialized with a string message and a sequence (typically a list) of exception instances. Groups can be nested to form a tree structure.Handling with except*
To handle exceptions, Python introduces the except* syntax. Unlike a standard except clause, which matches the exception object as a whole and stops, except* can structurally split exception groups and handle multiple branches independently.
except*:
- Leaf-Node Evaluation: Exception matching in an
except*clause is strictly evaluated only on the individual non-group exceptions (the leaves). The matching condition is never evaluated against theExceptionGrouporBaseExceptionGroupcontainers themselves. - Type Restrictions: Because
except*matching applies only to leaf nodes, it is aTypeErrorto useExceptionGroup,BaseExceptionGroup, or their subclasses as the target type in anexcept*clause. - Splitting and Wrapping: If leaf exceptions match the specified type, the exception group is structurally split. The matched leaf exceptions are extracted into a new
ExceptionGroupthat preserves the original nested shape, which is then bound to the variable in theasclause. If a standard, single exception is caught, Python automatically wraps it in anExceptionGroupbefore binding it. - Fallthrough: Any unmatched leaf exceptions remaining in the original group are propagated to subsequent
except*clauses. If exceptions remain after allexcept*blocks are evaluated, a newExceptionGroupcontaining the unhandled exceptions is implicitly re-raised. - Exclusivity: You cannot mix standard
exceptandexcept*clauses in the sametryblock.
Internal API
Exception groups expose specific methods and attributes for programmatic inspection and manipulation:exceptions: A tuple containing the direct child exceptions of the group.split(condition): Structurally splits the exception group. Theconditioncan be an exception type, a tuple of exception types, or a callable that takes an exception instance and returns a boolean. Unlike theexcept*syntax, the condition passed tosplit()is evaluated against exception group containers. If the condition returnsTruefor an exception group itself, the entire group is included in the match, and its individual child exceptions are not evaluated further. It returns a tuple(match, rest), wherematchis anExceptionGroupcontaining the exceptions that satisfy the condition, andrestis anExceptionGroupcontaining the remainder. If no exceptions match,matchisNone; if all match,restisNone.subgroup(condition): Similar tosplit(), but returns only thematchcomponent (anExceptionGrouporNone).
Master Python with Deep Grasping Methodology!Learn More





