null参数的IllegalArgumentException或NullPointerException? [英] IllegalArgumentException or NullPointerException for a null parameter?
问题描述
我有一个简单的属性setter方法, null
不适合这个特定的属性。在这种情况下我总是被撕裂:我应该抛出一个 IllegalArgumentException
,或 NullPointerException
?从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
.
这篇关于null参数的IllegalArgumentException或NullPointerException?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!