ArithmeticError
Python Programming Language
Severity: ModerateWhat Does This Error Mean?
ArithmeticError is the parent class for errors caused by math operations going wrong. The most common subtypes are ZeroDivisionError (dividing by zero), OverflowError (a number too large for Python to handle), and FloatingPointError (a floating-point calculation failed). You will rarely see ArithmeticError directly — you will usually see one of its subtypes.
Affected Models
- Python 2.x
- Python 3.x
- All Python versions
Common Causes
- Dividing a number by zero — raises ZeroDivisionError (a subclass of ArithmeticError)
- A math result so large it cannot be represented — raises OverflowError (a subclass of ArithmeticError)
- A floating-point operation fails at the hardware level — raises FloatingPointError (rare, disabled by default in most Python builds)
- Integer division by zero using the // operator
- Using modulo (%) with zero as the divisor
How to Fix It
-
Identify the specific subtype in the error message — it will say ZeroDivisionError, OverflowError, or FloatingPointError.
Each has a different fix. ZeroDivisionError is most common by far.
-
For ZeroDivisionError: add a check before dividing. Never divide without confirming the denominator is not zero.
Example: if denominator != 0: result = numerator / denominator else: result = 0 — or whatever makes sense for your program.
-
For OverflowError: check whether the number you are working with has grown unexpectedly large. This is rare with Python integers (which can be arbitrarily large) but can happen with floats.
Python's float type has a maximum value of about 1.8 × 10^308. If you go above that, you get an OverflowError.
-
To catch all math errors in one block, catch ArithmeticError — it will catch ZeroDivisionError, OverflowError, and FloatingPointError all at once.
Example: except ArithmeticError as e: print(f'Math error: {e}')
-
If you are doing math on user-supplied values, always validate the input before performing calculations. Check for zero denominators, negative numbers where they are not allowed, and unreasonably large values.
Never trust user input to be mathematically valid — users will enter 0, negative numbers, and text where you expect numbers.
When to Call a Professional
ArithmeticError is always something you can fix yourself. The most common cause is dividing by zero — check that your divisor can never be zero before doing division. Add a guard: if denominator != 0: before dividing.
Frequently Asked Questions
What is the difference between ArithmeticError and ZeroDivisionError?
ZeroDivisionError is a specific type of ArithmeticError — it is a child class. ArithmeticError is the general category; ZeroDivisionError is the specific case of dividing by zero. Catching ArithmeticError catches ZeroDivisionError, OverflowError, and FloatingPointError all at once.
Why can Python integers not overflow but floats can?
Python integers (int type) can grow to any size — Python automatically uses more memory as needed. But Python floats (float type) use the hardware's 64-bit floating-point format, which has a fixed maximum. So int(10) ** 10000 works fine, but float(10) ** 10000 raises OverflowError.
What is FloatingPointError and when does it happen?
FloatingPointError happens when a floating-point operation fails at the hardware level. In most Python builds it is disabled by default — floating-point errors are silently ignored and return special values like inf or nan instead. You will rarely see it unless you specifically enable floating-point exceptions with the fpectl module, which is also rarely used.