空参数的 IllegalArgumentException 或 NullPointerException? [英] IllegalArgumentException or NullPointerException for a null parameter?

查看:26
本文介绍了空参数的 IllegalArgumentException 或 NullPointerException?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的属性设置方法,null 不适用于这个特定的属性.在这种情况下,我一直被撕裂:我应该抛出一个 IllegalArgumentExceptionNullPointerException?从 javadocs 来看,两者似乎都很合适.是否有某种可以理解的标准?或者这只是您应该随心所欲地做的事情之一,而且两者都是正确的?

I have a simple setter method for a property and null is not appropriate for this particular property. I have always been torn in this situation: should I throw an IllegalArgumentException, or a NullPointerException? From the javadocs, both seem appropriate. Is there some kind of an understood standard? Or is this just one of those things that you should do whatever you prefer and both are really correct?

推荐答案

如果您不希望 null 成为允许的,则似乎会调用 IllegalArgumentException值,如果您尝试使用一个结果为 null 的变量,则会抛出 NullPointerException.

It seems like an IllegalArgumentException is called for if you don't want null to be an allowed value, and the NullPointerException would be thrown if you were trying to use a variable that turns out to be null.

这篇关于空参数的 IllegalArgumentException 或 NullPointerException?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆