raise 與 raise ... from 的區別


起步

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

參考

來源:https://segmentfault.com/a/1190000017332255


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM