Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

How to rethrow Python exception with new type?

I have the following code and I want to rethrow the exception with new type

try:
    1/0

except Exception as e:


1 Answer
Rajendra Dharmkar

In Python 3.x, the code is subject to exception chaining and we get the output as follows

C:/Users/TutorialsPoint1/~.py
Traceback (most recent call last):
File "C:/Users/TutorialsPoint1/~.py", line 2, in <module>
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 <module>
raise ValueError ( "Sweet n Sour grapes" ) from e
ValueError: Sweet n Sour grapes

Advertisements

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.