涨姿势了,raise...from... 是个什么操作?
source link: http://mp.weixin.qq.com/s?__biz=MzA5MTkxNTMzNg%3D%3D&%3Bmid=2650266907&%3Bidx=3&%3Bsn=5bfd32ad51d515c00cd10011550bcc72
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
点击上方 蓝色小字 ,关注 “ 涛哥聊Python ”
重磅干货,第一时间送达
作者:weapon
原文:https://zhuanlan.zhihu.com/p/52091476
1. 问题现象
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
2. 问题分析
不同之处在于, from
会为异常对象设置 __cause__
属性表明异常的是由谁直接引起的。
处理异常时发生了新的异常,在不使用 from
时更倾向于新异常与正在处理的异常没有关系。而 from
则是能指出新异常是因旧异常直接引起的。这样的异常之间的关联有助于后续对异常的分析和排查。 from
语法会有个限制,就是第二个表达式必须是另一个异常类或实例。
如果在异常处理程序或 finally 块中引发异常,默认情况下,异常机制会隐式工作会将先前的异常附加为新异常的 __context__
属性。
当然,也可以通过 with_traceback()
方法为异常设置上下文 __context__
属性,这也能在 traceback
更好的显示异常信息。
raise Exception("foo occurred").with_traceback(tracebackobj)
3. 禁止异常关联
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
4. 总结一下
在异常处理程序或 finally
块中引发异常,Python 会为异常设置上下文,可以手动通过 with_traceback()
设置其上下文,或者通过 from
来指定异常因谁引起的。这些手段都是为了得到更友好的异常回溯信息,打印清晰的异常上下文。若要忽略上下文,则可以通过 raise ... from None
来禁止自动显示异常上下文。
往期阅读
【Python版】昨晚停网后,我写了一段代码破解了隔壁小姐姐的wifi密码...
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK