ProgrammingHardTCS NQT Prime · Infosys SP

What is printed?

What is printed?
try {
    try { throw new Exception("1"); }
    finally { throw new RuntimeException("2"); }
} catch (Exception e) {
    System.out.print(e.getMessage() + " " + e.getSuppressed().length);
}
  1. A. 2 0
  2. B. 1 0
  3. C. 1 1
  4. D. 2 1

Correct answer: A. 2 0

The exception thrown in finally replaces the pending one (it is discarded, not recorded as suppressed), and the outer catch handles the RuntimeException.

More OOP with C++ and Java questions

Exams that ask this topic