我有一个简单的setter方法的属性和空是不适合这个特定的属性。在这种情况下,我总是被撕裂:我应该抛出一个IllegalArgumentException,还是一个NullPointerException?从javadocs来看,两者都很合适。是否存在某种公认的标准?或者这只是其中一件事,你应该做任何你喜欢做的事情,两种都是正确的?
当前回答
在这种情况下,IllegalArgumentException使用API向用户传达了“不应该为空”的明确信息。正如其他论坛用户指出的那样,只要你使用API向用户传达正确的信息,你就可以使用NPE。
GaryF和tweak放弃了“有效Java”(我发誓)的参考,建议使用NPE。看看其他好的API是如何构造的,这是了解如何构造你的API的最好方法。
Another good example is to look at the Spring APIs. For example, org.springframework.beans.BeanUtils.instantiateClass(Constructor ctor, Object[] args) has a Assert.notNull(ctor, "Constructor must not be null") line. org.springframework.util.Assert.notNull(Object object, String message) method checks to see if the argument (object) passed in is null and if it is it throws a new IllegalArgumentException(message) which is then caught in the org.springframework.beans.BeanUtils.instantiateClass(...) method.
其他回答
理想情况下,不应该抛出运行时异常。应该为您的场景创建一个受控异常(业务异常)。因为如果这些异常中的任何一个被抛出并记录下来,它就会误导开发人员在查看日志时。相反,业务异常不会造成这种恐慌,并且在故障排除日志时通常会被忽略。
您应该抛出一个IllegalArgumentException,因为这将使程序员清楚地知道他做了一些无效的事情。开发人员太习惯于看到VM抛出的NPE,以至于任何程序员都不会立即意识到自己的错误,并开始随机查看,或者更糟糕的是,指责你的代码“有bug”。
我想从其他非法参数中挑出Null参数,所以我从IAE派生了一个名为NullArgumentException的异常。甚至不需要读取异常消息,我就知道一个空参数被传递到一个方法中,并且通过读取消息,我找到了哪个参数为空。我仍然用IAE处理程序捕获NullArgumentException,但在我的日志中,我可以快速看到差异。
以上两个例外的链接定义如下 IllegalArgumentException:抛出该异常,表示方法被传递了一个非法或不适当的参数。 NullPointerException:当应用程序试图在需要对象的情况下使用null时抛出。
这里最大的区别是IllegalArgumentException应该在检查方法的参数是否有效时使用。当一个对象被“使用”为空时,就应该使用NullPointerException。
我希望这能帮助你正确看待这两者。
我完全赞成为空参数抛出IllegalArgumentException,直到今天,当我注意到Java 7中的Java .util. objects . requirenonnull方法时。用这种方法,而不是做:
if (param == null) {
throw new IllegalArgumentException("param cannot be null.");
}
你可以:
Objects.requireNonNull(param);
如果你传递给它的参数为空,它会抛出一个NullPointerException。
考虑到这个方法正好在java的中间。util我认为它的存在是一个非常强烈的迹象,抛出NullPointerException是“Java做事的方式”。
我想我已经决定了。
注意,关于硬调试的参数是虚假的,因为您当然可以向NullPointerException提供一条消息,说明什么是null,以及为什么它不应该是null。就像IllegalArgumentException。
One added advantage of NullPointerException is that, in highly performance critical code, you could dispense with an explicit check for null (and a NullPointerException with a friendly error message), and just rely on the NullPointerException you'll get automatically when you call a method on the null parameter. Provided you call a method quickly (i.e. fail fast), then you have essentially the same effect, just not quite as user friendly for the developer. Most times it's probably better to check explicitly and throw with a useful message to indicate which parameter was null, but it's nice to have the option of changing that if performance dictates without breaking the published contract of the method/constructor.
推荐文章
- Java 8接口方法中不允许“同步”的原因是什么?
- 如何找到Java堆大小和内存使用(Linux)?
- 使用Enum实现单例(Java)
- RabbitMQ与通道和连接之间的关系
- buildSessionFactory()配置方法在Hibernate中已弃用?
- Spring MVC -如何获得所有的请求参数在一个地图在Spring控制器?
- 如何在Java中按两个字段排序?
- Android -如何覆盖“后退”按钮,所以它不完成()我的活动?
- 文件之间的差异。路径中的分隔符和斜杠
- 为什么不使用异常作为常规的控制流呢?
- 在方法参数中使用NotNull注释
- Spring MVC中处理可选参数的@RequestParam
- Tomcat:如何查找正在运行的Tomcat版本?
- “java”、“javaw”和“javaws”之间有什么区别?
- 将Date对象转换为日历对象