Exception Methods
BaseExceptionGroup.subgroup() — exception method
Filter a group by exception type, tuple of types, or predicate. A matching nested group is retained as a whole; otherwise its children are examined and the surviving hierarchy is preserved. An empty match produces None. A callable predicate may receive a group itself, not just leaf exceptions.
Formal Syntax & Structure
python syntaxpython
group.subgroup(condition, /)Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| condition | type | tuple[type, ...] | callable | Exception class, tuple of exception classes, or non-type predicate. |
Return Value
BaseExceptionGroup | None
Matching subgroup or None.
Working Example
Example Snippetpython
group = ExceptionGroup("batch", [ValueError("row 1"), ExceptionGroup("row 2", [TypeError("price"), ValueError("quantity")])])match = group.subgroup(ValueError)print(match.message)print(len(match.exceptions))print(type(match.exceptions[1].exceptions[0]).__name__)print(group.subgroup(KeyError))