Group of BaseException instances (Python 3.11+)
Quick Answer
Use except* syntax (Python 3.11+) to handle individual exception types within a BaseExceptionGroup without catching unrelated errors.
Production Risk
Medium — unhandled BaseExceptionGroup will propagate all contained exceptions.
BaseExceptionGroup (Python 3.11+) wraps multiple BaseException instances — including KeyboardInterrupt and SystemExit — in a single exception. Use ExceptionGroup for Exception subclasses only; use BaseExceptionGroup when you need to group any BaseException.
- 1asyncio.gather / TaskGroup collecting multiple concurrent task failures including KeyboardInterrupt
- 2Libraries that wrap non-Exception base exceptions together
Fix
Use except* to handle grouped exceptions individually
try:
raise BaseExceptionGroup('signals', [
KeyboardInterrupt(),
SystemExit(1),
])
except* KeyboardInterrupt:
print('interrupted')
except* SystemExit as eg:
print('exit requested:', eg.exceptions)Why this works
except* unwraps the group and routes each exception type to the matching handler, re-raising unmatched types.
# ExceptionGroup — only Exception subclasses
ExceptionGroup('errs', [ValueError('bad'), TypeError('wrong')])
# BaseExceptionGroup — any BaseException including KeyboardInterrupt
BaseExceptionGroup('mixed', [KeyboardInterrupt(), ValueError('bad')])Both ExceptionGroup and BaseExceptionGroup were added in Python 3.11 via PEP 654.
Python 3.11 — PEP 654
Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev