SCJP 1.6版考題 132

出自 陳富國維基館
前往: 導覽搜尋
Given:
1. class TestException extends Exception{}
2. class A{
3.   public String sayHello(String name) throws TestException{
4.     if(name == null) throw new TestException(),
5.     return "Hello " + name;
6.   }
7. }
8. public class TestA{
9.   public static void main(String[] args){
10.   new A().sayHello("Aiko");
11. }
12.}

Which statement is true?
 A. Compilation succeeds.
 B. class A does not compile.
 C. The method declared on line 9 cannot be modified to throw TestException.
 D. TestA compiles if line 10 is enclosed in try/catch block that catches TestException.

解答


Ans: D

解說:

sayHello宣告會丟出TestException,呼叫sayHello的敘述必須放在try/catch中
或者宣告... main throws TestException (String ...