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 catch ZeroDivisionError Exception in Python?

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?


1 Answer
Rajendra Dharmkar

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

Advertisements

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