从中提出的链异常

在处理异常的过程中,你可能希望引发另一个异常。例如,如果在从文件读取时获得 IOError,则可能需要引发特定于应用程序的错误以呈现给库的用户。

Python 3.x >= 3.0

你可以链接异常以显示如何处理异常:

>>> try:
    5 / 0
except ZeroDivisionError as e:
    raise ValueError("Division failed") from e

Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
ZeroDivisionError: division by zero

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "<stdin>", line 4, in <module>
ValueError: Division failed