public class ExceptionTest { public static void main(String[] args) {
public class ExceptionTest {
public static void main(String[] args) {
try {
System.out.println("Result: " + calculate(10, 0));
} catch (ArithmeticException e) {
System.out.println("ArithmeticException caught: " + e.getMessage());
} catch (Exception e) {
System.out.println("General exception caught: " + e.getMessage());
} finally {
System.out.println("In finally block.");
}
}
public static int calculate(int a, int b) throws Exception {
try {
return a / b;
} catch (ArithmeticException e) {
throw new Exception("Division by zero not allowed");
} finally {
System.out.println("Inner finally block executed.");
}
}
}
What will be the output of the program?
Show answerHide answer
Correct answer: (B) Inner finally block executed. General exception caught: Division by zero not allowed In finally block.
Explanation
10/0 throws ArithmeticException inside calculate, which is caught and replaced by new Exception("Division by zero not allowed"). The inner finally prints first, then main's catch (Exception) prints that message, and finally main's finally block runs.