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

NameTypeDescription
conditiontype | tuple[type, ...] | callableException 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))
Python Reference