Dev HelpersOpen library
← Back

Handle task group exceptions by type

Handle task group exceptions by type in Python.

Open this example in the interactive library

Source code

import asyncio
async def fail():
    raise ValueError('invalid')
async def main():
    try:
        async with asyncio.TaskGroup() as group:
            group.create_task(fail())
    except* ValueError as errors:
        print(len(errors.exceptions))
asyncio.run(main())

Sample output

1