python - 在 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
问题:
- 是否可以避免使用另一个变量(在我的示例中
exc1
)同时仍然避免UnboundLocalError
? - 为什么
except <Exception> as <var>
语句“吞下”已经定义的局部变量?
解决方案
这种情况在8.4 中描述。尝试语句:
当使用 as target 分配异常时,它会在 except 子句的末尾被清除。这仿佛
except E as N: foo
被翻译成
try: foo finally: del N
这意味着必须将异常分配给不同的名称才能在 except 子句之后引用它。异常被清除是因为附加了回溯,它们与堆栈帧形成一个引用循环,使该帧中的所有本地人保持活动状态,直到下一次垃圾回收发生。
推荐阅读
- java - Hibernate 将数字(保存十进制值)数据类型从 DB 映射到 Java 中的 Long
- eclipse-rse - 在 RSE 中如何创建系统过滤器
- verilog - 如何理解 Cadence XMVLOG 编译器支持哪个 SystemVerilog?
- django - 使用 gmail 作为客户端(django、石墨烯)发送电子邮件失败
- c# - 如何获取wopi主机集成的file_id?
- highcharts - 如何在 xAxis 格式化程序上放置 highcharts 符号?
- mongodb - 检查 mongodb 连接字符串在 asp.net 核心中是否有效
- python - Python - 请求 - POST 有效负载
- reactjs - 启动使用 create-react-app 引导的反应应用程序时出错
- python - 在 Python 中解析非零填充的 12 小时日期时间格式