raise 與 raise ... from 的區別

起步

Python 的 raiseraise from 之間的區別是什麼?html

try:
    print(1 / 0)
except Exception as exc:
    raise RuntimeError("Something bad happened")

輸出:python

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 fromapp

try:
    print(1 / 0)
except Exception as exc:
    raise RuntimeError("Something bad happened") from exc

輸出:code

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__ 屬性代表異常的是由誰直接引發的。htm

處理異常時發生了新的異常,在不使用 from 時更傾向於新異常與正在處理的異常沒有關聯。而 from 則是能指出新異常是因舊異常直接引發的。這樣的異常之間的關聯有助於後續對異常的分析和排查。from 語法會有個限制,就是第二個表達式必須是另外一個異常類或實例。對象

若是在異常處理程序或 finally 塊中引起異常,默認狀況下,異常機制會隱式工做會將先前的異常附加爲新異常的 __context__ 屬性。get

固然,也能夠經過 with_traceback() 方法爲異常設置上下文 __context__ 屬性,這也能在 traceback 更好的顯示異常信息。it

raise Exception("foo occurred").with_traceback(tracebackobj)

禁止異常關聯

from 還有個特別的用法:raise ... from None ,它經過設置 __suppress_context__ 屬性指定來明確禁止異常關聯:io

try:
    print(1 / 0)
except Exception as exc:
    raise RuntimeError("Something bad happened") from None

輸出:ast

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 來禁止自動顯示異常上下文。

參考

相關文章
相關標籤/搜索