Fixing java.lang.ArithmeticException: / by zero in Java

beginnerโ˜• Java2026-07-22| Java Development Kit (JDK) / Java Runtime Environment (JRE) - All Versions

Error Message

java.lang.ArithmeticException: / by zero
#java#exception-handling#arithmetic#debugging#backend

Why This Error Happens

If your Java code tries to divide an integer by zero, the JVM immediately halts execution and throws a java.lang.ArithmeticException: / by zero. Think of it this way: trying to split 100 apples among 0 people doesn't just result in "nothing." In the world of integer math, it is an impossible operation that the language refuses to process.

This crash specifically targets integer types like int, long, short, and byte. Interestingly, floating-point numbers (double and float) behave differently. They follow the IEEE 754 standard, which means they return Infinity or NaN (Not a Number) instead of throwing an exception and killing your program.

Code that triggers the crash:

int totalScore = 500;
int players = 0;
int average = totalScore / players; // Boom: java.lang.ArithmeticException: / by zero

The same rule applies to the modulo operator. If you try to find the remainder of a division by zero, the program will fail:

int remainder = 10 % 0; // Also throws java.lang.ArithmeticException

How to Fix It

1. Validate the Divisor First

The most reliable fix is to check your numbers before the math happens. This is a must-have check whenever you deal with user input, such as a value entered into a web form or a number pulled from a database.

public void calculateShare(int total, int people) {
    if (people != 0) {
        int share = total / people;
        System.out.println("Each person gets: " + share);
    } else {
        System.err.println("Calculation skipped: You can't divide by zero people.");
    }
}

2. Use a Try-Catch Block

Sometimes you expect your data to be clean, and a zero is a genuine "exception" to the rule. In these cases, wrap the logic in a try-catch block to handle the failure gracefully without crashing the whole application.

try {
    int result = 100 / divisor;
} catch (ArithmeticException e) {
    System.out.println("Error: Division by zero occurred. Defaulting to 0.");
    int result = 0; 
}

3. Use the Ternary Operator for Default Values

If you want to keep your code concise, use a ternary operator. This is perfect for setting a default value (like 0 or 1) when the denominator is missing.

// If players is 0, set average to 0; otherwise, perform the division
int average = (players == 0) ? 0 : (totalScore / players);

4. Switch to Floating-Point Math

Does your logic require decimal precision? If you cast your numbers to double, the exception disappears. Instead of a crash, you get Infinity, which might be easier to handle in some scientific or financial calculations.

double result = (double) 10 / 0; 
System.out.println(result); // Prints 'Infinity'

Testing Your Fix

Before deploying your code, run through these three scenarios to ensure stability:

  • Positive scenarios: Does 10 / 2 still return 5?
  • Zero scenarios: Does the app stay running when the divisor is 0? Verify that your custom error message or default value appears.
  • Negative scenarios: Does 10 / -2 correctly return -5?

Key Takeaways

  • Sanitize Input: Never assume a UI or API will send non-zero numbers. Always validate before calculating.
  • Contextual Logging: If you catch this exception, log the specific variables involved. Knowing that userId: 402 caused a zero-division error makes debugging much faster.
  • Choose Your Type: Remember that 1.0 / 0.0 is a valid mathematical state (Infinity) in Java, but 1 / 0 is a fatal error.

Related Error Notes