🎙 Free live masterclass · Sun, 29 Nov, 7:00 pm IST · Java Full Stack roadmap Register free →
Skills101
← 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());
        }
    }
}
  1. A.after
  2. B.caught x
  3. C.after then caught x
  4. 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 →