HiveBrain v1.2.0
Get Started
← Back to all entries
debugjavaCritical

IllegalArgumentException or NullPointerException for a null parameter?

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
illegalargumentexceptionparameternullpointerexceptionfornull

Problem

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?

Solution

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.

Context

Stack Overflow Q#3881, score: 315

Revisions (0)

No revisions yet.