我在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