我有一个简单的setter方法的属性和空是不适合这个特定的属性。在这种情况下,我总是被撕裂:我应该抛出一个IllegalArgumentException,还是一个NullPointerException?从javadocs来看,两者都很合适。是否存在某种公认的标准?或者这只是其中一件事,你应该做任何你喜欢做的事情,两种都是正确的?
当前回答
二分法……它们不重叠吗?只有整体中不重叠的部分才能构成二分法。在我看来:
throw new IllegalArgumentException(new NullPointerException(NULL_ARGUMENT_IN_METHOD_BAD_BOY_BAD));
其他回答
理想情况下,不应该抛出运行时异常。应该为您的场景创建一个受控异常(业务异常)。因为如果这些异常中的任何一个被抛出并记录下来,它就会误导开发人员在查看日志时。相反,业务异常不会造成这种恐慌,并且在故障排除日志时通常会被忽略。
Apache Commons Lang有一个NullArgumentException,它完成了这里讨论的许多事情:它扩展了IllegalArgumentException,并且它唯一的构造函数采用了参数的名称,而参数的名称本应该是非空的。
虽然我觉得抛出NullArgumentException或IllegalArgumentException之类的异常更准确地描述了异常情况,但我和同事们还是选择遵从Bloch在这个问题上的建议。
公认的做法是使用IllegalArgumentException(字符串消息)来声明一个参数是无效的,并给出尽可能多的细节…所以说,一个参数被发现是空的,而异常是非空的,你会这样做:
if( variable == null )
throw new IllegalArgumentException("The object 'variable' cannot be null");
你几乎没有理由隐式地使用“NullPointerException”。NullPointerException是当您试图在空引用(如toString())上执行代码时由Java虚拟机抛出的异常。
如果您选择抛出NPE,并且在方法中使用参数,显式检查null可能是多余的和昂贵的。我想VM已经为你做了。
给杰森·科恩的论点投了一票,因为它表现得很好。让我一步一步地分解它。: -)
The NPE JavaDoc explicitly says, "other illegal uses of the null object". If it was just limited to situations where the runtime encounters a null when it shouldn't, all such cases could be defined far more succinctly. Can't help it if you assume the wrong thing, but assuming encapsulation is applied properly, you really shouldn't care or notice whether a null was dereferenced inappropriately vs. whether a method detected an inappropriate null and fired an exception off. I'd choose NPE over IAE for multiple reasons It is more specific about the nature of the illegal operation Logic that mistakenly allows nulls tends to be very different from logic that mistakenly allows illegal values. For example, if I'm validating data entered by a user, if I get value that is unacceptable, the source of that error is with the end user of the application. If I get a null, that's programmer error. Invalid values can cause things like stack overflows, out of memory errors, parsing exceptions, etc. Indeed, most errors generally present, at some point, as an invalid value in some method call. For this reason I see IAE as actually the MOST GENERAL of all exceptions under RuntimeException. Actually, other invalid arguments can result in all kinds of other exceptions. UnknownHostException, FileNotFoundException, a variety of syntax error exceptions, IndexOutOfBoundsException, authentication failures, etc., etc.
总的来说,我觉得NPE受到了很大的诋毁,因为传统上一直与未能遵循快速失效原则的代码联系在一起。再加上JDK未能用消息字符串填充NPE,这确实产生了一种强烈的负面情绪,这种情绪是没有根据的。实际上,从运行时的角度来看,NPE和IAE之间的区别仅限于名称。从这个角度来看,你的名字越精确,你给调用者的信息就越清晰。
推荐文章
- Java 8接口方法中不允许“同步”的原因是什么?
- 如何找到Java堆大小和内存使用(Linux)?
- 使用Enum实现单例(Java)
- RabbitMQ与通道和连接之间的关系
- buildSessionFactory()配置方法在Hibernate中已弃用?
- Spring MVC -如何获得所有的请求参数在一个地图在Spring控制器?
- 如何在Java中按两个字段排序?
- Android -如何覆盖“后退”按钮,所以它不完成()我的活动?
- 文件之间的差异。路径中的分隔符和斜杠
- 为什么不使用异常作为常规的控制流呢?
- 在方法参数中使用NotNull注释
- Spring MVC中处理可选参数的@RequestParam
- Tomcat:如何查找正在运行的Tomcat版本?
- “java”、“javaw”和“javaws”之间有什么区别?
- 将Date对象转换为日历对象