Special Method Protocols

__aexit__() — async protocol

Return an awaitable for leaving an asynchronous context manager. The resolved truth value determines exception suppression. The arguments describe the exception or are all None on normal exit.

Formal Syntax & Structure

python syntaxpython
async def __aexit__(self, exc_type, exc_value, traceback):

Parameters & Attributes

NameTypeDescription
exc_typetype | NoneException type.
exc_valueBaseException | NoneException object.
tracebacktraceback | NoneTraceback.

Return Value

awaitable

Awaitable resolving to suppression decision. The coroutine returned by this async def is awaited by the protocol.

Working Example

Example Snippetpython
import asyncioclass Session:    async def __aenter__(self): return self    async def __aexit__(self, exc_type, exc_value, traceback): return exc_type is KeyErrorasync def demo():    async with Session(): raise KeyError("missing")    return "continued"print(asyncio.run(demo()))
Python Reference