Ad Space — Top Banner

ArithmeticException

Java Programming Language

Severity: Minor

What Does This Error Mean?

An ArithmeticException means your code tried to do a math operation that is not mathematically valid. The most common cause by far is dividing by zero: 10 / 0. Division by zero is undefined in mathematics, so Java throws this error instead of producing a nonsense result. The fix is to check that your divisor is not zero before dividing.

Affected Models

  • Java 8
  • Java 11
  • Java 17
  • Java 21
  • All Java versions

Common Causes

  • Integer division by zero — using the / or % operator when the divisor is 0
  • The divisor variable is zero because of unexpected input, a calculation that went wrong, or an uninitialized variable
  • Using the modulo operator (%) with a zero divisor — 10 % 0 throws the same error as 10 / 0
  • BigDecimal operations that produce a non-terminating decimal when using division without specifying a scale
  • An off-by-one error in a loop that allows the denominator to reach zero

How to Fix It

  1. Find the division operation in your code at the line shown in the stack trace. Identify the variable used as the divisor (the number after the / symbol).

    The stack trace shows the exact file and line number. Go directly there — do not guess.

  2. Add a check before the division: if (divisor != 0) { result = numerator / divisor; } else { handle the zero case. }

    Decide what should happen when the divisor is zero — use a default value, return early, throw a meaningful exception, or log a warning.

  3. Trace back to where the divisor value comes from. If it comes from user input, database data, or a calculation, add validation at that point to prevent a zero value from reaching the division.

    Validating early (at the input point) is better than checking at every division — it catches the problem at the source.

  4. For floating-point division (using double or float), dividing by zero does not throw an exception — it returns Infinity or NaN. Check for these: Double.isInfinite(result) and Double.isNaN(result).

    This is a Java quirk: only integer division throws ArithmeticException. Floating-point division produces special values instead of crashing.

  5. For BigDecimal division, always specify a scale and rounding mode: bigDecimal.divide(divisor, 2, RoundingMode.HALF_UP). Without these, a repeating decimal like 1/3 throws ArithmeticException.

    RoundingMode.HALF_UP is the familiar 'round half up' rule. Choose the rounding mode that fits your application's requirements.

When to Call a Professional

ArithmeticException is almost always fixable by adding a simple check before dividing. The error message is usually 'ArithmeticException: / by zero' — it is very specific. For BigDecimal division errors, you need to specify a rounding mode.

Frequently Asked Questions

Why does Java crash on integer division by zero but not floating-point division by zero?

The IEEE 754 floating-point standard defines special values like Infinity and NaN for these cases. Java follows that standard for double and float. But integers have no special values — there is no integer representation for 'infinity'. So Java throws an exception instead of producing a meaningless integer result.

What is NaN and when does it appear?

NaN stands for 'Not a Number'. It appears from floating-point operations that have no meaningful numeric result, like 0.0 / 0.0 or Math.sqrt(-1). NaN has a strange property: NaN != NaN is true — a NaN value is not equal to itself. Use Double.isNaN(value) to check for NaN.

Can I catch ArithmeticException and continue?

Yes — ArithmeticException is unchecked, and you can catch it with try/catch. But catching it instead of preventing it is usually the wrong approach. A divide-by-zero almost always means there is a logic error upstream. Fix the root cause rather than swallowing the exception.