ArithmeticError

ArithmeticError is a built-in exception that serves as the base class for all errors that occur during arithmetic operations. Python offers more specific exceptions that inherit from ArithmeticError.

You typically won’t raise ArithmeticError directly in your code. Instead, you’ll encounter its subclasses:

  • ZeroDivisionError
  • OverflowError
  • FloatingPointError

These subclasses provide more specific information about what went wrong during the arithmetic operation.

ArithmeticError Occurs When

  • Performing an arithmetic operation that doesn’t map cleanly onto existing built-in subclasses like ZeroDivisionError or OverflowError.
  • Introducing a custom domain-specific arithmetic operation that results in an undefined or invalid numeric state and wanting to create your own subclass of ArithmeticError.

ArithmeticError Can Be Used When

  • Raise ArithmeticError temporarily as a catch-all if you’re quickly prototyping and want to signal that some unspecified arithmetic went wrong.
  • Use ArithmeticError in an except block to catch multiple different exceptions that are subclasses of ArithmeticError.

However, in general you should avoid raising ArithmeticError directly and instead raise one of its subclasses to clearly communicate the nature of the problem. If you need to report an arithmetic issue that doesn’t map cleanly onto Python’s built-in subclasses, then you should subclass ArithmeticError to create your own domain-specific exception type.

ArithmeticError Example

You typically won’t get or raise ArithmeticError in your code because this exception is designed to be a base class. If you have a piece of code that can raise different subclasses of ArithmeticError, then you can catch this exception as a generic one:

Python
>>> def calculate_effective_interest_rate(nominal_rate, periods):
...     try:
...         # Formula: (1 + r/n)^n - 1
...         effective_rate = (1 + nominal_rate / periods) ** periods - 1
...         return effective_rate
...     except ArithmeticError as e:
...         print(f"Error in calculation: {e!r}")
...         # Return NaN to indicate an invalid result
...         return float('nan')
...

>>> calculate_effective_interest_rate(0.05, 12)
0.051161897881732976
>>> calculate_effective_interest_rate(0.05, 0)
Error in calculation: ZeroDivisionError('float division by zero')
nan
>>> calculate_effective_interest_rate(1000, 10_000)
Error in calculation: OverflowError(34, 'Result too large')
nan

Depending on the arguments you use to call calculate_effective_interest_rate(), you can either get a ZeroDivisionError or OverflowError exception. Using ArithmeticError in the except block, you can catch either exception, which is great when the handling code is common for both exceptions.

Tutorial

Python Exceptions: An Introduction

In this beginner tutorial, you'll learn what exceptions are good for in Python. You'll see how to raise exceptions and how to handle them with try ... except blocks.

basics python

For additional information on related topics, take a look at the following resources:


By Leodanis Pozo Ramos • Updated March 11, 2025 • Reviewed by Martin Breuss