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());
}- A. wrap:null
- B. io-fail:wrap
- C. wrap:io-fail
- 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
- What happens when this runs? java class Test { static void m() { throw new ArithmeticException("boom"); } public static void main(String[]…
- What is printed? java static void m(int x) throws java.io.IOException, InterruptedException { if (x == 0) throw new java.io.IOException()…
- What happens when this runs, assuming recurse() causes infinite recursion? java static void recurse() { recurse(); } public static void…
- What is printed? java class AEx extends Exception {} class BEx extends AEx {} // in main, wrapped in throws Exception: try { throw new…
- Does this compile (Java 7+)? java static void m() throws java.io.IOException, java.sql.SQLException { try { if (Math.random() < 2) throw…
- Does this compile? java class A { void m() throws java.io.IOException {} } class B extends A { @Override void m() throws…