Exceptions: int division by zero throws, double division produces Infinity or NaN

Integer division by zero always throws ArithmeticException because there is no representable integer result, while floating-point division by zero instead produces the special values Infinity, -Infinity or NaN.

Code
try {
    int result = 5 / 0;
    System.out.println("Unreachable: " + result);
} catch (ArithmeticException e) {
    System.out.println("int: " + e.getClass().getSimpleName() + ": " + e.getMessage());
}
System.out.println("double 5.0/0.0: " + (5.0 / 0.0));
System.out.println("double 0.0/0.0: " + (0.0 / 0.0));
Output
int: ArithmeticException: / by zero
double 5.0/0.0: Infinity
double 0.0/0.0: NaN
Advertisement
More in JAVA

Run this yourself in the Online Java Compiler, spin up a live REST API in the API Sandbox, or practise with Java interview questions.

Published 2026-09-27

© Java Coding Hub · About · Contact · Privacy · Terms