Hello,
I got this question from my quiz :
Consider the following code (assume that comments are replaced with real code that works as specified):
public class TestExceptions {
static void e() {
// Might cause any of the following unchecked exceptions to be
// thrown:
// Ex1, Ex2, Ex3, Ex4
}
static void April() {
try {
e();
} catch (Ex1 ex) {
System.out.println("April caught Ex1");
}
}
static void March() {
try {
April();
} catch (Ex2 ex) {
System.out.println("March caught Ex2");
// now cause exception Ex1 to be thrown
}
}
static void February() {
try {
March();
} catch (Ex1 ex) {
System.out.println("February caught Ex1");
} catch (Ex3 ex) {
System.out.println("February caught Ex3");
}
}
static void a() {
try {
February();
} catch (Ex4 ex) {
System.out.println("January caught Ex4");
// now cause exception Ex1 to be thrown
} catch (Ex1 ex) {
System.out.println("January caught Ex1");
}
}
public static void main(String[] args) {
January();
}
}
Assume now that this program is run four times. The first time, method e throws exception Ex1, the second time, it throws exception Ex2, etc.
What are the results of the four runs (a or b)?
And the answer is :
The program prints:
April caught Ex1
The program prints:
March caught Ex2
February caught Ex1
The program prints:
February caught Ex3
The program prints:
January caught Ex4
And execution stops due to an uncaught exception Ex1 thrown in main()
Can somebody tell me the explanation about this ?
Thank you