Question 79
Given:
11. static void test() throws Error { 
12. if (true) throw new AssertionError(); 
13. System.out.print(!±test !°)
14. } 
15. public static void main(String[] args) { 
16. try { test(); } 
17. catch (Exception ex) { System.out.print(!±exception !°); 
18. System.out.print(!±elld !°)
19. } What is the result? 
A. end 
B. Compilation fails. 
C. exception end 
D. exception test end 
E. A Throwable is thrown by main. 
F. An Exception is thrown by main. 
Answer: E

解决方案 »

  1.   


    问题一:我在eclipse运行了以后,错误提示是:
    Exception in thread "main" java.lang.AssertionError
    at Test.test(Test.java:3)
    at Test.main(Test.java:8)
    是Exception被抛出而不是Throwable啊!!所以怎么会是E呢?
      

  2.   

    答:抛出的异常是:AssertionError ,它不是Exception 的子类.因而:
    try { test(); } 
    catch (Exception ex) { System.out.print(!±exception !°); 
    是抓不到该AssertionError的,而是直接抛给了JVM.
    因而是E.
      

  3.   


    但是我在CMD下测试,javac 命令没有错误,java 命令就提示:
    Exception in thread "main" java.lang.NoClassDefFoundError: Test{我的测试类类名}如何理解这句话里的“Exception”而不是“throwable”谢谢
      

  4.   


    可是你扔出的不是Error吗
      

  5.   

    Throwable是Error和Exception的子类,AssertionError是Error的子类,用AssertionError,Error,Throwable都可以捕获AssertionError,而Exception与AssertionError没有任何父子类关系,因而不能捕获。
      

  6.   


    强烈反对此种宣传,唉,本十分仰慕java2000_net的,可是回复了我的帖子全是这个广告,太让我失望了!!!!!!!!!!!!