Untitled
unknown
plain_text
a year ago
810 B
6
Indexable
public class exception_handling {
public static void main(String[] args) {
// Declare and initialize variables
int a = 10, b = 5, c = 5, x, y;
try {
// Attempt to divide a by the result of (b - c)
// This will throw an ArithmeticException because b - c = 0, resulting in division by zero.
x = a / (b - c);
} catch (ArithmeticException e) {
// Catch and handle the exception
System.out.println("divide BY ZERO\n" + e); // Print the exception message
}
// After the try-catch block, continue with normal execution
// Calculate the value of y
y = (a / b + c); // a / b = 10 / 5 = 2, then 2 + c = 2 + 5 = 7
System.out.println("y = " + y); // Output the value of y
}
}
Editor is loading...
Leave a Comment