Course contentsShow
← Java
Lesson 801 of 2,87019. Exception Hierarchy and Custom ExceptionsFree lesson

ArithmeticException: Division by Zero and Integer Overflow

Learn which arithmetic operations throw ArithmeticException and which silently overflow.

ArithmeticException: Division by Zero and Integer Overflow

What you'll learn: You'll discover which arithmetic operations throw ArithmeticException and which silently overflow without warning.

The Tale of Two Arithmetic Failures

Not all math errors behave the same way in Java. Some scream for attention by throwing exceptions, while others fail silently—and you need to know which is which.

Division by Zero: The Exception Thrower

When you divide an integer by zero, Java throws an ArithmeticException immediately:

int result = 10 / 0;  // Throws ArithmeticException at runtime

This also applies to the modulo operator:

int remainder = 10 % 0;  // Throws ArithmeticException

Important: Floating-point division (double or float) does not throw this exception—it produces special values like Infinity or NaN instead. Only integer division (int, long, etc.) throws ArithmeticException.

Integer Overflow: The Silent Saboteur

Here's the surprising part: when integer arithmetic exceeds the maximum or minimum value, Java does not throw an exception. It silently wraps around:

int max = Integer.MAX_VALUE;  // 2,147,483,647
int overflow = max + 1;       // -2,147,483,648 (wraps to minimum)
// No exception thrown!

Think of it like an odometer rolling over from 999,999 back to 000,000. Java keeps computing, producing incorrect results without complaint.

When to Catch ArithmeticException

Since division by zero is the primary trigger, catch ArithmeticException when performing division or modulo operations with user input or calculated denominators:

try {
    int quotient = numerator / denominator;
} catch (ArithmeticException e) {
    System.out.println("Cannot divide by zero");
}

Key Takeaway: ArithmeticException is thrown for integer division/modulo by zero, but integer overflow silently wraps around without throwing any exception—making overflow bugs particularly dangerous.