ArithmeticError Exception in Python
Handle Arithmetic error exception in Python using try-except block.

The ArithmeticError
Exception is the base class for all errors associated with arithmetic operation.
ArithmeticError
--> ZeroDivisionError
--> OverflowError
--> FloatingPointError
Example 1 - Handling ZeroDivisionError
Code
try:
1/0
except ArithmeticError as e:
print(f"{e}, {e.__class__}")
Output
division by zero, <class 'ZeroDivisionError'>
As you can see, the ArithmeticError
exception class is able to handle ZeroDivisionError
exception. The e.__class__
method tells you that it was a ZeroDivisionError
.
Example 2 - Handling OverflowError
Code
j = 5.0
try:
for i in range(1, 1000):
j = j**i
except ArithmeticError as e:
print(f"{e}, {e.__class__}")
Output
(34, 'Result too large'), <class 'OverflowError'>
As you can see, by using the ArithmeticError
exception class, you can handle both ZeroDivisionError
and OverflowError
exceptions.
Use this exception class, anytime you are unsure of any arithmetic operations and the errors that it might result in.
Check out other Python Built-in Exception classes in Python.
built-in-exception-classes - Pylenin
A programmer who aims to democratize education in the programming world and help his peers achieve the career of their dreams.
