except* for Exception Groups
PY · Modern Featuressyntax
try:
...
except* ErrorType as eg:
...example
import asyncio
async def task_a():
raise ValueError("bad value")
async def task_b():
raise TypeError("wrong type")
async def main():
try:
async with asyncio.TaskGroup() as tg:
tg.create_task(task_a())
tg.create_task(task_b())
except* ValueError as eg:
print(f"Value errors: {eg.exceptions}")
except* TypeError as eg:
print(f"Type errors: {eg.exceptions}")
# asyncio.run(main())output
Value errors: (ValueError('bad value'),)
Type errors: (TypeError('wrong type'),)Note except* can match multiple handlers for the same ExceptionGroup. It splits the group so each handler gets only its matching exceptions.