我在Python except
子句中有一些代码用于执行某些日志记录,但日志记录代码本身可能会导致异常。在我的情况下,我想忽略任何可能发生的第二个异常,并引发原始异常。这是非常简单的例子:处理在except子句中发生的Python异常
try:
a = this_variable_doesnt_exist
except:
try:
1/0
except:
pass
raise
运行上面的代码,我希望能得到:
NameError: name 'this_variable_doesnt_exist' is not defined
而是在Python 2.x中,我得到:
ZeroDivisionError: integer division or modulo by zero
我发现在Python 3.x中,它做我想要的。
我无法在Python 2.x文档中找到关于此的很多评论(除非我错过了它)。我可以在2.x中实现这个吗?
如何明确处理次要异常?请在你的回答中加入这个 – 2017-04-03 23:13:41