Skip to content

Docs: the Reference says the exception is not available in a finally clause, but sys.exception() and a bare raise see it #162601

Backlog

Description

@Robertboy18

Documentation

The finally section of the Reference (https://docs.python.org/dev/reference/compound_stmts.html#finally) says: "The exception information is not available to the program during execution of the finally clause." But in a finally clause run because of an exception, sys.exception() returns that exception and a bare raise re-raises it (test_raise.TestRaise.test_finally_reraise relies on the second):

import sys
try:
    try:
        raise KeyError("pending")
    finally:
        print("in finally:", repr(sys.exception()))
        raise
except KeyError as e:
    print("re-raised:", repr(e))

CPython 3.15.2 and main (639c34b):

in finally: KeyError('pending')
re-raised: KeyError('pending')

I think the sentence should go, or say what is available. I found it while writing a Lean spec of Python.

Linked PRs

Activity

  1. added
    AI-needs-explanation
    on Nov 25, 2026
  2. bedevere-app commented on Nov 25, 2026

    @bedevere-app

    Thank you for taking the time to open this issue. Python is a community-driven project, and we want it to be a welcoming place to learn and collaborate.

    This issue appears to have been written with AI assistance. Please explain your issue in your own words and describe how you checked the information you provided. This helps us understand your issue and leaves more time to address other issues.

    Please read our guidelines for using AI tools.

  3. added
    docs
    and removed
    AI-needs-explanation
    on Nov 25, 2026
  4. picnixz commented on Nov 25, 2026

    @picnixz
    Member

    I think we can just remove the sentence or specify "may not be avaiable". There might be cases where there isn't any exception available but this needs investigation to be sure about it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    docs

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions