CKTIS / Python
ZeroDivisionError — division by zero
ZeroDivisionError is a built-in exception raised when division or a remainder operation uses a zero divisor. Handle it with except ZeroDivisionError when the program can respond meaningfully to that case.
Version: Python 3.14; repository image, production runtime not attested
Syntax
try:
result = numerator / denominator
except ZeroDivisionError:
...ZeroDivisionError is a subclass of ArithmeticError. For built-in int and float operands, /, //, and % raise this exception when they check the divisor and find that it is zero. With mixed int and float operands, conversion can fail first. For example, (10**400) / 0.0 raises OverflowError while converting the numerator to float, before the divisor is checked. An except ZeroDivisionError: block does not catch that error. Zero on the left is not a problem when the divisor is nonzero.
This is a runtime error, not a syntax error. When the exception occurs inside a try block, the remaining statements in that block are skipped. A matching except ZeroDivisionError: block handles it; if no matching handler is found, execution stops with an error message.
Keep the try block focused on the operation requiring protection and catch the specific exception rather than the broad Exception. Do not substitute zero for a failed result unless the program's requirements justify it. The message text depends on the operands and operation—identify the ZeroDivisionError class, not its message text.
Examples
for denominator in (2, 0):
try:
result = 8 / denominator
except ZeroDivisionError:
print("ZeroDivisionError")
else:
print(result)Result
4.0 ZeroDivisionError
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.