ArithmeticError Exception in Python

By Lenin Mishra

This is the part of the 9th day in the Python 30 series. Check out the series here.

The ArithmeticError Exception is the base class for all errors associated with arithmetic operation.

-> ArithmeticError
--> ZeroDivisionError
--> OverflowError
--> FloatingPointError
Example 1 - Handling ZeroDivisionError


except ArithmeticError as e:
    print(f"{e}, {e.__class__}")


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


j = 5.0

    for i in range(1, 1000):
        j = j**i
except ArithmeticError as e:
    print(f"{e}, {e.__class__}")


(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.

  1. Try, Except, Else and Finally in Python
  2. ZeroDivisionError Exception in Python
  3. OverflowError Exception in Python
  4. KeyError Exception in Python
  5. IndexError Exception in Python
  6. LookupError Exception in Python
  7. StopIteration Exception in Python
  8. TypeError Exception in Python
  9. NameError Exception in Python
  10. FileNotFoundError Exception in Python
  11. Catch Multiple Exceptions in Python