ProgrammingHardIBM · Deloitte pattern

What is printed?

What is printed?
try {
    try {
        throw new java.io.IOException("io-fail");
    } catch (java.io.IOException e) {
        throw new RuntimeException("wrap", e);
    }
} catch (RuntimeException e) {
    System.out.print(e.getMessage() + ":" + e.getCause().getMessage());
}
  1. A. wrap:null
  2. B. io-fail:wrap
  3. C. wrap:io-fail
  4. D. null:io-fail

Correct answer: C. wrap:io-fail

RuntimeException("wrap", e) sets "wrap" as this exception's own message and e (the IOException) as its cause, so getMessage() returns "wrap" and getCause().getMessage() returns "io-fail": "wrap:io-fail".

More OOP with C++ and Java questions

Exams that ask this topic