In this code example, an ArithmeticException
is thrown when dividing by 0. Besides the actual error message, a user-defined one is output.
public class TryCatchArithmeticException {
public static void main(String[] args) {
int dividend = 10;
int divisor = 0;
try {
System.out.println(dividend + " / " + divisor + " = " + dividend / divisor);
} catch (ArithmeticException e) {
System.out.println("Division by 0 is not possible");
System.out.println("Error: " + e);
}
}
}
Division by 0 is not possible
Error: java.lang.ArithmeticException: / by zero