How to catch ZeroDivisionError Exception in Python?

PythonServer Side ProgrammingProgramming

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.

Example

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

raja
Published on 06-Dec-2017 12:26:16
Advertisements