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:
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
ZeroDivisionErrororOverflowError. - Introducing a custom, domain-specific arithmetic operation that results in an undefined or invalid numeric state, and you want to create your own subclass of
ArithmeticErrorto represent that condition.
ArithmeticError Can Be Used When
- You want a temporary catch-all during quick prototyping and just want to signal that something arithmetic-related went wrong.
- You need to catch multiple arithmetic exceptions at once by using
ArithmeticErrorin anexceptblock to group its subclasses.
However, in general, you should avoid raising ArithmeticError directly and raise one of its subclasses instead to make the specific problem clear. If you need to signal an arithmetic issue that doesn’t fit any of the built-in subclasses, then create your own domain-specific exception by subclassing ArithmeticError.
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:
>>> 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. By using ArithmeticError in the except block, you can catch either exception, which is helpful when the handling code is the same for each case.
Related Resources
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.
For additional information on related topics, take a look at the following resources:
- Python's raise: Effectively Raising Exceptions in Your Code (Tutorial)
- Understanding the Python Traceback (Tutorial)
- Introduction to Python Exceptions (Course)
- Raising and Handling Python Exceptions (Course)
- Python Exceptions: An Introduction (Quiz)
- Using raise for Effective Exceptions (Course)
- Python's raise: Effectively Raising Exceptions in Your Code (Quiz)
- Getting the Most Out of a Python Traceback (Course)