首页 > 解决方案 > 在 finally 块中使用异常变量

问题描述

运行此示例函数时:

from typing import Tuple, Any, Optional

def func() -> Tuple[Any, Optional[Exception]]:
    exc = None
    ret = None
    try:
        # code here, if successful assign result to `ret`
        ret = "Result"
        # comment this line out and the code works
        raise Exception
    except Exception as exc:
        exc.__traceback__ = None
        # Error logging here
        pass
    finally:
        return ret, exc

print(func())  # expected: ("Result", <Exception instance>)

最后一行 ( return ret, exc) 引发UnboundLocalError: local variable 'exc' referenced before assignment,即使 thoexc明确地绑定在函数 ( exc = None) 的第一行。这可以通过更改except-clause 来解决,如下所示:

except Exception as exc1:
    exc = exc1
    exc.__traceback__ = None
    # Error logging here
    pass

问题

  1. 是否可以避免使用另一个变量(在我的示例中exc1)同时仍然避免UnboundLocalError
  2. 为什么except <Exception> as <var>语句“吞下”已经定义的局部变量?

标签: pythonpython-3.xexceptionscopetry-catch

解决方案


这种情况在8.4 中描述。尝试语句

当使用 as target 分配异常时,它会在 except 子句的末尾被清除。这仿佛

except E as N:
   foo

被翻译成

   try:
       foo
   finally:
       del N

这意味着必须将异常分配给不同的名称才能在 except 子句之后引用它。异常被清除是因为附加了回溯,它们与堆栈帧形成一个引用循环,使该帧中的所有本地人保持活动状态,直到下一次垃圾回收发生。


推荐阅读