Python Glossary Русский

Python 3.11+Exceptionsexception

ExceptionGroup

built-in exceptions

A group of several exceptions at once (3.11+); handled with except*.

Syntax

raise ExceptionGroup

Group membership is not transparent: except ValueError will not catch a group that contains a ValueError, and except ExceptionGroup will not catch a plain ValueError raised on its own. That is what except* is for — it splits the group by type, runs the branch for the matching subset and re-raises the rest; with an ordinary except you get the whole object and must walk e.exceptions yourself. The constructor demands a non-empty sequence whose items all derive from Exception; use BaseExceptionGroup for anything below that. In practice you meet groups on the way out of asyncio.TaskGroup far more often than in your own raise.

Examples

try:
    raise ExceptionGroup('несколько ошибок', [ValueError('a'), KeyError('b')])
except ExceptionGroup as e:
    print(type(e).__name__)   # → ExceptionGroup

See also

Open in the full glossarydocs.python.orgReport an inaccuracy