SCJP 1.6版考題 132

出自 陳富國維基館
於 2013年3月30日 (六) 03:43 由 Ikk (對話 | 貢獻) 所做的修訂 (新頁面: 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....)
(差異) ←上個修訂 | 最新修訂 (差異) | 下個修訂→ (差異)
前往: 導覽搜尋
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 ...