exceptiongroup
A backport of PEP 654, providing exception groups and the 'except*' syntax for Python versions prior to 3.11. Current version: 1.3.1. Released on November 21, 2025. Maintained by The Trio Collective.
Warnings
- breaking The 'except*' syntax introduced in PEP 654 is not available in Python versions prior to 3.11. Ensure compatibility with your Python version.
- gotcha The 'catch' function from 'exceptiongroup' is necessary to handle exception groups in Python versions prior to 3.11. Omitting it will result in unhandled exceptions.
Install
-
pip install exceptiongroup
Imports
- BaseExceptionGroup
from exceptiongroup import BaseExceptionGroup
- ExceptionGroup
from exceptiongroup import ExceptionGroup
- catch
from exceptiongroup import catch
Quickstart
import asyncio
from exceptiongroup import BaseExceptionGroup, catch
async def read_file(filename):
with open(filename) as f:
data = f.read()
return data
async def main():
try:
async with asyncio.TaskGroup() as g:
g.create_task(read_file('unknown1.txt'))
g.create_task(read_file('unknown2.txt'))
print('All done')
except* FileNotFoundError as eg:
for e in eg.exceptions:
print(e)
asyncio.run(main())