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