Built-in Exceptions

StopAsyncIteration — exception class

Signals exhaustion of an asynchronous iterator. async for handles this signal internally, and anext(iterator, default) can provide a fallback. Do not use ordinary StopIteration for async iteration.

Formal Syntax & Structure

python syntaxpython
StopAsyncIteration

Return Value

type

The class object StopAsyncIteration; this reference documents the class identity, not a universal constructor signature.

Working Example

Example Snippetpython
import asyncioasync def empty():    if False: yield 1async def exhaust(): await anext(empty())try:    asyncio.run(exhaust())except StopAsyncIteration as error:    print(type(error).__name__)
Python Reference