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 do you handle an exception thrown by an except clause in Python?

I have code where an except clause throws an exception.

try:
    a = john
except:
    try:
        4/0
    except:
        pass

How to handle this exception?


1 Answer
Rajendra Dharmkar

We have a scenario in which code in except clause itself raises exception. In given code, we can handle the exception raised by except clause as follows.

import sys
try:
a = john
except:
try:
4/0
except:
print sys.exc_info()

We get the following output

"C:/Users/TutorialsPoint1/~.py"
(<type 'exceptions.ZeroDivisionError'>, ZeroDivisionError('integer division or modulo by zero',), <traceback object at 0x0000000002BD4B88>)
Advertisements

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