019-004-003
Exception Types: Handling Division by Zero
Easy
Problem Description
In this problem, you will create a program that catches an ArithmeticException thrown by integer division by zero using try-catch and displays the error message to standard output.
ArithmeticException
ArithmeticException is thrown when an arithmetic operation error occurs.
Common Cases
int a = 10 / 0; // ArithmeticException thrown // Message: / by zero
Handling
try {
Int result = a / b;
} catch (ArithmeticException e) {
System.out.println("Error: " + e.getMessage());
}Learning Points
- Integer division by zero throws ArithmeticException
- Floating-point division by zero gives Infinity, no exception
- Use e.getMessage() to get error details
Ready to Try Running Code?
Log in to access the code editor and execute your solutions for this problem.
Don't have an account?
Sign Up