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」。