Given:
11. class X{public void foo(){System.out.print("X ");}}
12.
13. public class SubB extends X{
14. public void foo() throws RuntimeException{
15. super.foo();
16. if(true) throw new RuntimeException();
17. System.out.print("B ");
18. }
19. public static void main(String[] args){
20. new SubB().foo();
21. }
22. }
What is the result?
A. X, followed by an Exception.
B. No output, and an Exception is thrown.
C. Compilation fails due to an error on line 14.
D. Compilation fails due to an error on line 16.
E. Compilation fails due to an error on line 17.
F. X, followed by an Exception, followed by B.
答案:A
參考:9-3 使用throw自行產生例外事件、9-5 使用throws拋出例外事件。
--------------------------------------------------------------------
20. new SubB().foo();
>>14. public void foo() throws RuntimeException{
>>15. super.foo(); //x
>>>>16. if(true) throw new RuntimeException(); //Exception
>>>>20. new SubB().foo();
沒有留言:
張貼留言