文件名称:捕获异常后抛出另外的异常-python cookbook(第3版)高清中文完整版
文件大小:4.84MB
文件格式:PDF
更新时间:2024-06-29 23:06:52
python cookbook 第3版 高清 中文完整版
14.9 捕获异常后抛出另外的异常
问题
You want to raise an exception in response to catching a different exception, but want to
include information about both exceptions in the traceback.
解决方案
To chain exceptions, use the raise from statement instead of a simple raise statement. This
will give you information about both errors. For example:
>>> def example():
... try:
... int('N/A')
... except ValueError as e:
... raise RuntimeError('A parsing error occurred') from e...
>>>
example()
Traceback (most recent call last):
File "