Class TestException 
1. public class TestException extends Exception { 
2. } 
Class A: 
1. public class A { 
2. 
3. public String sayHello(String name) throws TestException { 
4. 
5. if(name == null) { 
6. throw new TestException();
7. } 
8. 
9. return “Hello “+ name
10. } 
11. 
12. } 
A programmer wants to use this code in an application: 
45. A a=new A(); 
46. System.out.println(a.sayHello(“John”))
Which two are true? (Choose two.) 
A. Class A will not compile. 
B. Line 46 can throw the unchecked exception TestException. 
C. Line 45 can throw the unchecked exception TestException. 
D. Line 46 will compile if the enclosing method throws a TestException. //问题一:如果在Line6处抛出异常的话,Line9还能够被编译成功。是吗?为什么会编译成功而不是在异常处直接路过不编译异常后面的语句呢???要是我理解有错,怎样理解呢?
E. Line 46 will compile if enclosed in a try block, where TestException 
is caught. //问题二:我理解的意思是如果Line16改到Line6与Line7之间时,能够编译成功???这样理解对吗?要不应该怎样理解呢?谢谢。。
Answer: DE 问题三:BC为什么是错的呢?谢谢

解决方案 »

  1.   

    1、类的编译是在调用之前,并且并不是所有情况都 throw 异常,return还是能执行到了,所以不会编译通不过。2、Line16改到Line6与Line7。没有Line16啊。你的意思是不是在 throw 后面,添加 return 啊? 那样return语句是永远都不会执行的,编译通不过。3、46行编译都通不过,这是前提
      

  2.   

    1、异常是你运行程序的时候抛出的
    2、因为a.sayHello方法有可能抛出TestException异常,所以调用的地方要放在try...catch...块中或者调用的方法把该异常继续往上抛
      

  3.   

    答:
    1)编译时,只好不是死代码(即:永远执行不到的代码),都要进行编译与目标代码生成的.Line9 不是死代码(即:Line 6若不抛出异常,则Line9是会执行的.),因而是要进行编译并生成目标代码的. 
    2)若Line46改到Line6与Line7之间时,则Line46是死代码,是编译时会出现"警告",但会编译成功.但编译时,由于Line46是死代码,故编译时Line46实际上已被删除了,不参加目标代码的生成.
    3)
    B--错在:TestException是一个checked exception 而 不是一个unchecked exception (只有:Error类及其子类,RuntimeException类及其子类,才是unchecked exception )
    C--错在:Line 45 不会抛出任何异常.