Exception Attributes

BaseException.__context__ — exception attribute

Inspect the exception that was being handled when a second exception was raised. Implicit chaining retains the original failure even when an explicit cause or from None changes the displayed traceback. A newly constructed exception has no context until one is attached.

Formal Syntax & Structure

python syntaxpython
error.__context__

Return Value

BaseException | None

Previous exception object, or None.

Working Example

Example Snippetpython
try:    try:        int("bad")    except ValueError:        raise RuntimeError("configuration")except RuntimeError as error:    print(type(error.__context__).__name__)    print(error.__cause__ is None)
Python Reference