Python ZeroDivisionError Exception
Example
A ZeroDivisionError
occurs if you try to divide a number with 0:
print(10 / 0)
Try it Yourself »
Definition and Usage
The ZeroDivisionError
exception occurs when you try to devide a number with 0,
and when you perform a modulo operation with 0.
The ZeroDivisionError
exception is one of three ArithmeticError
You can handle the ZeroDivisionError
in a try...except
statement, see the example below.
Modulo With Zero
A ZeroDivisionError
occurs if you try to perform a modulo operation with 0:
Exception Handling
Example
Handling the ZeroDivisionError
in a try...except
statement:
try:
print(10 / 0)
except ZeroDivisionError:
print("Error in calculation")
except:
print("Something else went wrong")
Try it Yourself »