SCJP題庫第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 ...

Comments

Popular posts from this blog

Android+Google Map API v3 Geocoding(地址轉經緯度度