Why do I get a `NameError` (or `UnboundLocalError`) from using a named exception after the `except` block?

The try statement explicitly limits the scope of the bound exception, to prevent circular references causing it to leak.

When an exception has been assigned using as target, it is cleared at the end of the except clause.

[…]

This means the exception must be assigned to a different name to be able to refer to it after the except clause. Exceptions are cleared because with the traceback attached to them, they form a reference cycle with the stack frame, keeping all locals in that frame alive until the next garbage collection occurs.

Emphasis mine; to use the exception afterward, it must be bound to a new name. exc = exc will not help because except isn’t creating a new scope, but instead the name specified in the except statement is being removed from the scope.

In Python 2, exceptions did not have a reference to the traceback, so this clearing was not necessary to prevent the garbage collection problem. Now they do, so it changed.

However, even in Python 2, you are explicitly warned about cleaning up tracebacks:

Warning: Assigning the traceback return value to a local variable in a function that is handling an exception will cause a circular reference. This will prevent anything referenced by a local variable in the same function or by the traceback from being garbage collected. Since most functions don’t need access to the traceback, the best solution is to use something like exctype, value = sys.exc_info()[:2] to extract only the exception type and value. If you do need the traceback, make sure to delete it after use (best done with a try ... finally statement) or to call exc_info() in a function that does not itself handle an exception.

If you do re-bind the exception, you may want to clear the traceback explicitly:

try:
    raise Exception("foo")
except Exception as e:
    exc = e
    exc.__traceback__ = None

Leave a Comment