← Quiz: Java Exception Handling: all 10 questions
Quiz: Java Exception Handling · Question 7 of 10
What does this print? (code)
What does this print?
public class Main {
static void a() { b(); }
static void b() { throw new IllegalStateException("x"); }
public static void main(String[] args) {
try {
a();
System.out.println("after");
} catch (IllegalStateException e) {
System.out.println("caught " + e.getMessage());
}
}
}
- A.after
- B.caught x
- C.after then caught x
- D.The program crashes
Answer & explanation
Correct answer: caught x
The exception is not caught in b or a, so it moves up the call stack to main, where the catch handles it. "after" is skipped.
Can you get the rest right without peeking?
Take the timed Quiz: Java Exception Handling quiz free. Pass it to earn XP and count towards your certificate.
Start the quiz →