Java ArithmeticException
Example
Divide a number by zero, and catch the exception:
public class Main {
public static void main(String[] args) {
int x = 10;
int y = 0;
try {
System.out.println(x / y);
} catch (ArithmeticException e) {
System.out.println("You cannot divide by zero.");
}
}
}
Definition and Usage
An ArithmeticException happens when a math calculation cannot be done. The most common cause is dividing a whole number (an int or a long) by zero.
Dividing a double by zero does not throw this exception. The result is Infinity or NaN (not a number) instead.
Related Pages
Java Exceptions (try...catch) Tutorial
❮ Errors & Exceptions