Given:
public class TestApp {
public static void main(String[] args) {
TestApp t = new TestApp();
try {
t.doPrint();
t.doList();
} catch (Exception e2) {
System.out.println("Caught " + e2);
}
}
public void doList() throws Exception {
throw new Error("Error");
}
public void doPrint() throws Exception {
throw new RuntimeException("Exception");
}
}
What is the result?
A.
Caught java.lang.RuntimeException: Exception
Exception in thread "main" java.lang.Error: Error
at TestApp.doList(TestApp.java: 14)
at TestApp.main(TestApp.java: 6)
Exception in thread "main" java.lang.Error: Error
at TestApp.doList(TestApp.java: 14)
at TestApp.main(TestApp.java: 6)
B.
Exception in thread "main" java.lang.Error: Error
at TestApp.doList(TestApp.java: 14)
at TestApp.main(TestApp.java: 6)
at TestApp.doList(TestApp.java: 14)
at TestApp.main(TestApp.java: 6)
C.
Caught java.lang.RuntimeException: Exception
Caught java.lang.Error: Error
Caught java.lang.Error: Error
D.
Caught java.lang.RuntimeException: Exception
題解
程式第 5 行會呼叫第 14 行的 doPrint 方法,然後拋出一個 RuntimeException 物件。這個 RuntimeException 會在程式第 7 行的 catch 被接住,然後輸出「Caught java.lang.RuntimeException: Exception」。
