【发布时间】:2016-10-08 16:32:29
【问题描述】:
在 Python 3 中,有一个 useful raise ... from ... feature 可以重新引发异常。也就是说,您如何从引发的异常中找到原始(/重新引发的)异常?这是一个带有 cmets 的(愚蠢的)示例来说明我的意思--
def some_func():
try:
None() # TypeError: 'NoneType' object is not callable
except as err:
raise Exception("blah") from err
try:
some_func()
except as err:
# how can I access the original exception (TypeError)?
【问题讨论】:
-
顺便说一句:使用
from在执行raise Exception("blah") from None时最有用,它告诉python 隐藏TypeError并使其无法访问。 默认情况下TypeError已经存储在新异常中(这就是为什么您在回溯中看到During handling of the above exception blah消息的原因)所以这样做raise ... from err几乎没用。
标签: python python-3.x exception exception-handling