起步
Python 的 raise
和 raise from
之間的區別是什么?
try:
print(1 / 0)
except Exception as exc:
raise RuntimeError("Something bad happened")
輸出:
Traceback (most recent call last):
File "test4.py", line 2, in <module>
print(1 / 0)
ZeroDivisionError: division by zero
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "test4.py", line 4, in <module>
raise RuntimeError("Something bad happened")
RuntimeError: Something bad happened
而 raise from
:
try:
print(1 / 0)
except Exception as exc:
raise RuntimeError("Something bad happened") from exc
輸出:
Traceback (most recent call last):
File "test4.py", line 2, in <module>
print(1 / 0)
ZeroDivisionError: division by zero
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "test4.py", line 4, in <module>
raise RuntimeError("Something bad happened") from exc
RuntimeError: Something bad happened
分析
不同之處在於,from
會為異常對象設置 __cause__
屬性表明異常的是由誰直接引起的。
處理異常時發生了新的異常,在不使用 from
時更傾向於新異常與正在處理的異常沒有關聯。而 from
則是能指出新異常是因舊異常直接引起的。這樣的異常之間的關聯有助於后續對異常的分析和排查。from
語法會有個限制,就是第二個表達式必須是另一個異常類或實例。
如果在異常處理程序或 finally
塊中引發異常,默認情況下,異常機制會隱式工作會將先前的異常附加為新異常的 __context__
屬性。
當然,也可以通過 with_traceback()
方法為異常設置上下文 __context__
屬性,這也能在 traceback
更好的顯示異常信息。
raise Exception("foo occurred").with_traceback(tracebackobj)
禁止異常關聯
from 還有個特別的用法:raise ... from None
,它通過設置 __suppress_context__
屬性指定來明確禁止異常關聯:
try:
print(1 / 0)
except Exception as exc:
raise RuntimeError("Something bad happened") from None
輸出:
Traceback (most recent call last):
File "test4.py", line 4, in <module>
raise RuntimeError("Something bad happened") from None
RuntimeError: Something bad happened
總結
在異常處理程序或 finally
塊中引發異常,Python 會為異常設置上下文,可以手動通過 with_traceback()
設置其上下文,或者通過 from
來指定異常因誰引起的。這些手段都是為了得到更友好的異常回溯信息,打印清晰的異常上下文。若要忽略上下文,則可以通過 raise ... from None
來禁止自動顯示異常上下文。