Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Python

Cách rethrow Python exception với type mới?

Trong Python 3.x, code chịu exception chaining và chúng ta nhận được output như sau:

C:/Users/TutorialsPoint1/~.py
Traceback (most recent call last):
  File "C:/Users/TutorialsPoint1/~.py", line 2, in 
    1/0
ZeroDivisionError: division by zero

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

Traceback (most recent call last):
  File "C:/Users/TutorialsPoint1/~.py", line 4, in 
    raise ValueError("Sweet n Sour grapes") from e
ValueError: Sweet n Sour grapes