ProgrammingVery hardProduct-company MCQ pattern

What is printed?

What is printed?
static void f() throws Exception {
    try {
        throw new RuntimeException("A");
    } finally {
        throw new IllegalStateException("B");
    }
}
public static void main(String[] a) {
    try { f(); } catch (Exception e) { System.out.print(e.getMessage()); }
}
  1. A. AB
  2. B. A
  3. C. B
  4. D. A then B on separate lines

Correct answer: C. B

When finally itself throws, that new exception replaces the one propagating from try; RuntimeException("A") is discarded (not suppressed, just lost) and only IllegalStateException("B") reaches main's catch.

More OOP with C++ and Java questions

Exams that ask this topic