java.lang.ArithmeticException in Java
On this page
java.lang.ArithmeticException Class
Thrown when an exceptional arithmetic condition has occurred. For example, an integer "divide by zero" throws an instance of this class.
Creating Objects
ArithmeticException arithmeticException = new ArithmeticException("Custom error message: Division by zero");
java.lang.ArithmeticException Class Example
public class ArithmeticExceptionExample {
public static void main(String[] args) {
try {
int numerator = 10;
// This will cause division by zero
int denominator = 0;
// This line will throw ArithmeticException
int result = numerator / denominator;
System.out.println("Result: " + result);
} catch (ArithmeticException e) {
// Handle the exception
System.out.println("Error: Cannot divide by zero!");
} finally {
System.out.println("Execution completed.");
}
}
}
Output:
Error: Cannot divide by zero!
Execution completed.