ArithmeticError
You never see it raised directly — it is the one except clause that covers division by zero and numeric overflow together.
ArithmeticError(*args)
Raised by
never directly — via ZeroDivisionError, OverflowError, FloatingPointError
Message
the subclass's: 'division by zero', 'int too large to convert to float'
Quick fix
except ArithmeticError: to cover both zero and overflow
Watch out
math.sqrt(-1) is ValueError ('math domain error'), not ArithmeticError
Demo
Live evaluation
Integer divmod: floor quotient and remainder. A zero divisor raises ZeroDivisionError — an ArithmeticError.
Try:
Inputs
astrdividend
bstrdivisor
Code
divmod(int('17'), int('5'))
Result
(3, 2)
The same handler returns ZeroDivisionError for a zero denominator and OverflowError for a 400-digit numerator — float() cannot hold a number that large. Python ints themselves never overflow: divmod in Trigger works on any length. Type a letter to see that int()'s ValueError is not an ArithmeticError and escapes.
Constructor
| Name | Type | Required | Description |
|---|---|---|---|
| *args | object | no | Normally one message string, stored in e.args. Raise a subclass instead of ArithmeticError itself. |
Attributes
| Attribute | Type | Meaning |
|---|---|---|
| args | tuple | The subclass's message, e.g. ('division by zero',). |
| __context__ | BaseException | None | The exception being handled when this one was raised. |
| __cause__ | BaseException | None | Set by raise ... from ... |
Common patterns
Guard a whole calculation
Report any numeric failure in a formula without listing every subclass.
try: growth = (new - old) / old * 100 except ArithmeticError: growth = None
Numeric and domain errors together
The math module uses ValueError for out-of-domain input, so a robust numeric guard catches both.
try: y = math.log(x) / math.sqrt(z) except (ArithmeticError, ValueError) as e: y = float('nan')
Examples
1. The three subclasses
[issubclass(c, ArithmeticError) for c in (ZeroDivisionError, OverflowError, FloatingPointError)]
Returns
[True, True, True]2. Catch a division by zero
try:
1 / 0
except ArithmeticError as e:
r = f'{type(e).__name__}: {e}'
r
Returns
'ZeroDivisionError: division by zero'3. Int too large for a float
float(10 ** 400)
Returns
OverflowError: int too large to convert to float4. math functions overflow too
import math
math.exp(1000)
Returns
OverflowError: math range error5. Ints never overflow
len(str(2 ** 10000))
Returns
30116. Float arithmetic overflows to inf silently
1e308 * 10
Returns
inf7. decimal errors are ArithmeticErrors
import decimal
issubclass(decimal.DivisionByZero, ArithmeticError)
Returns
TruePitfalls
1. Catching only ZeroDivisionError
A ratio can also fail with OverflowError when the int is too big for a float. The base class covers both.
except ZeroDivisionError
try: r = float(10 ** 400) / 3 except ZeroDivisionError: r = None r
OverflowError: int too large to convert to float
except ArithmeticError
try: r = float(10 ** 400) / 3 except ArithmeticError: r = 'not computable' r
'not computable'
2. math domain errors are ValueError
Square root or log of a negative number raises ValueError, which ArithmeticError does not catch.
except ArithmeticError
import math try: r = math.sqrt(-1) except ArithmeticError: r = 'bad input' r
ValueError: math domain error
catch both
import math try: r = math.sqrt(-1) except (ArithmeticError, ValueError): r = 'bad input' r
'bad input'
3. Waiting for OverflowError from float math
Plain float operators do not raise on overflow — they return inf. Check the result instead.
except OverflowError
try: x = 1e308 * 10 except OverflowError: x = 'overflow' x
inf
math.isfinite
import math x = 1e308 * 10 r = x if math.isfinite(x) else 'overflow' r
'overflow'
When to use
Use it
- One handler for any numeric failure in a formula
- Base class for your own numeric errors (subclass ArithmeticError or ValueError)
- Catching decimal-module signals like DivisionByZero together with built-in errors
Reach for something else
- You only expect a zero divisor → except ZeroDivisionError says so
- Invalid math input like sqrt(-1) → that is ValueError
- Detecting float overflow from * or ** of floats → check math.isfinite(result)
Notes
CPython impl
Objects/exceptions.c — ArithmeticError is a plain subclass of Exception with no extra behaviour
Subclasses
ZeroDivisionError, OverflowError, FloatingPointError (not raised by CPython in practice)
decimal
decimal.DivisionByZero also subclasses ZeroDivisionError; the other decimal signals subclass ArithmeticError via DecimalException
FAQ
The base class for arithmetic failures: ZeroDivisionError, OverflowError and FloatingPointError. Python never raises plain ArithmeticError itself; you use it in except clauses to catch any of its subclasses with one handler.