How to catch ZeroDivisionError Exception in Python?


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

Updated on: 12-Feb-2020

408 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements