Question 82
Given a method that must ensue 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”)
Answer: D请问各个答案有什么区别啊?也就是我搞不明白为什么D可以而其它答案不可以啊?谢谢
Given a method that must ensue 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”)
Answer: D请问各个答案有什么区别啊?也就是我搞不明白为什么D可以而其它答案不可以啊?谢谢
这个方法中的参数一定不能是null,如果是 null 的话,可能会抛出一些异常讯息 .
inserted at line 12, is the appropriate way to handle a null
问我们在12行处, 哪个选项是最好的处理这个null值的方式
非法的参数异常
让人看了就知道错误出在那里 肯定就选D了
而是你的编程习惯。总的来说
D选项比较完善一点。返回的数据清楚。可以准确判断错误的原因。
IllegalArgumentException
非法的参数异常