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 wanting to create your own subclass of
ArithmeticError.
ArithmeticError Can Be Used When
- Raise
ArithmeticErrortemporarily as a catch-all if you’re quickly prototyping and want to signal that some unspecified arithmetic went wrong. - Use
ArithmeticErrorin anexceptblock to catch multiple different exceptions that are subclasses ofArithmeticError.
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:
>>> 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.
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)