python异常链是什么

318次阅读
没有评论

python异常链是什么

1、说明

当我们通过except捕捉到一个异常A后,可以用raise语句再次抛出一个异常B。

然后我们看到的异常信息是B的信息。但我们不知道这个异常B来自哪里,此时,我们可以使用异常链。

在抛出异常链时,使用raisefrom语句。

2、实例

>>> def func():
...     raise IOError
...
>>> try:
...     func()
... except IOError as exc:
...     raise RuntimeError('Failed to open database') from exc
...
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
  File "<stdin>", line 2, in func
OSError
 
The above exception was the direct cause of the following exception:
 
Traceback (most recent call last):
  File "<stdin>", line 4, in <module>
RuntimeError: Failed to open database
神龙|纯净稳定代理IP免费测试>>>>>>>>天启|企业级代理IP免费测试>>>>>>>>IPIPGO|全球住宅代理IP免费测试

相关文章:

版权声明:wuyou2021-04-24发表,共计568字。
新手QQ群:570568346,欢迎进群讨论 Python51学习