all Python 3Exceptionsexception
ZeroDivisionError
built-in exceptions
Division or modulo by zero, e.g. 1/0 or 5 % 0.
Syntax
raise ZeroDivisionError
Unlike IEEE-754 or numpy, where dividing by zero yields inf, Python's built-in numeric types always raise — 1.0 / 0.0 fails exactly like 1 / 0. The same goes for 5 % 0, divmod(5, 0) and 0 ** -1. Checking the divisor up front is usually cleaner than wrapping a large block in try, since a wide except can hide a zero that leaked in from somewhere else.
Examples
try:
1 / 0
except ZeroDivisionError as e:
print(type(e).__name__) # → ZeroDivisionError
See also
Open in the full glossarydocs.python.orgReport an inaccuracy