raise 与 raise ... from 的区别

简介: 起步Python 的 raise 和 raise from 之间的区别是什么?try: print(1 / 0)except Exception as exc: raise RuntimeError("...

起步

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

相关文章
|
5月前
|
前端开发
【JCEF】关于-1 The query has been canceled或Unexpected call to CefQueryCallback_N::finalize()错误
【JCEF】关于-1 The query has been canceled或Unexpected call to CefQueryCallback_N::finalize()错误
58 0
|
3月前
|
Python
使用 raise_exception 装饰器,简化 if not ... raise ... 抛出异常的过程
使用 raise_exception 装饰器,简化 if not ... raise ... 抛出异常的过程
33 0
|
3月前
|
缓存
pytest 运行测试函数报错的解决办法 TypeError: calling <function xxx> returned None, not a test
pytest 运行测试函数报错的解决办法 TypeError: calling <function xxx> returned None, not a test
87 0
|
11月前
|
机器学习/深度学习 Windows
raise RuntimeError(‘Error(s) in loading state_dict for {}:\n\t{}‘.format( RuntimeError: Error(s)..报错
即load_state_dict(fsd,strict=False) 属性strict;当strict=True,要求预训练练权重层数的键值与新构建的模型中的权重层数名称完全吻合;
1143 0
|
PyTorch 算法框架/工具
torch中报错:AttributeError: 'builtin_function_or_method' object has no attribute 'detach'怎么解决?
这个错误信息 "AttributeError: 'builtin_function_or_method' object has no attribute 'detach'" 表示你尝试在一个内置函数或方法对象上调用 detach() 方法,而这种对象没有这个属性。 detach() 是 PyTorch 张量和变量的方法,允许它们从计算图中分离出来,因此不能在其他类型的对象上调用。要解决这个错误,请确保你正在一个 PyTorch 张量或变量上调用 detach() 方法。
836 0
|
计算机视觉
成功解决TypeError: __init__() got an unexpected keyword argument 'n_iterations'
成功解决TypeError: __init__() got an unexpected keyword argument 'n_iterations'
|
物联网 Linux 开发者
Raise 函数|学习笔记
快速学习 Raise 函数
84 0
Raise 函数|学习笔记
|
Python
SyntaxError: Missing parentheses in call to 'print'
SyntaxError: Missing parentheses in call to 'print'
107 0
|
PyTorch 算法框架/工具
raise NotImplementedError
raise NotImplementedError
136 0