If the following line of code is run at the python interpreter, I get an error.
x =11/0
How to catch this error and know the type of error?
When zero shows up in the denominator of a division operation, a ZeroDivisionError is raised.
We re-write the given code as follows to handle the exception and find its type.
import sys try: x = 11/0 print x except Exception as e: print sys.exc_type print e
OUTPUT
<type 'exceptions.ZeroDivisionError'> integer division or modulo by zero
We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy. Accept Learn more