SCJP 1.6版考題 139

出自 陳富國維基館
前往: 導覽搜尋
Given a method that must ensure that its parameter is not null:
11. public void someMethod(Object value){
12.   //check for null value
...
20.   System.out.println(value.getClass());
21. }


What, inserted at line 12, is the appropriate way to handle a null value?
 A. assert value == null;
 B. assert value != null : "value is null";
 C. if (value == null){
        throw new AssertionException("value is null");
       }
 D. if (value == null){
        throw new IllegalArgumentException("value is null");
       }

解答


Ans: D

解說:

value是公開的方法someMethod中的參數,此參數會由類別外部給予值,類別無法控制此值…
這種性質的參數測試不適合用asserttion(因assertion可以隨時關掉)