• Post Reply Bookmark Topic Watch Topic
  • New Topic

IllegalArgumentException vs NullPointerException  RSS feed

 
Roel De Nijs
Sheriff
Posts: 11338
177
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Assume you have a public method with 1 parameter. A null value is inappropriate for this method parameter. Do you throw an IllegalArgumentException (IAE) or a NullPointerException (NPE) or maybe even another exception? And why do you throw that exception?

If you look at the Java API itself, both seem appropriate. Wondering if there is some kind of standard I'm not aware of... Or maybe it's nothing more but a style question where some people will defend their preference very passionately

For what it's worth, I'm using IAE. For no particular reason. It's the exception I always used/use when an illegal argument is passed to a method (I like consistency in the code I'm working on )
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would certainly throw IllegalArgumentException and include a helpful message if possible. Much more informative than NPE - consider the work you would have to do with an NPE to figure out what happened.

As many beginners have discovered it is possible to track an NPE to a statement and still have no idea what happened.

Bill

 
Campbell Ritchie
Marshal
Posts: 56581
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The API recommends throwing an NPE whenever you pass an inappropriate null, so I would go for an NPE. Or, since Java7:-I put in a spelling error so I wouldn't use the same String literal twice. And if you believe that you will believe anything!
 
Guillermo Ishi
Ranch Hand
Posts: 789
C++ Linux Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
npe is more specific in your example than illegal argument which is an "argument" for throwing npe
 
Paul Anilprem
Enthuware Software Support
Ranch Hand
Posts: 4116
34
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To me, there is one major issue with throwing NPE and ArrayIndexOutOfBoundsException. These exceptions indicate that the violation has already occurred...i.e. you have already tried to access a null pointer or tried to access an element beyond a valid index. But this is obviously not the case if you are checking for null (and the index). There is no violation. IllegalArgumentException is pre facto. It reflects the right coding approach. For this reason alone, I prefer to throw IllegalArgumentException whenever the code prevents such invalid access. It correctly reports the situation as it is.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
William Brogden wrote:I would certainly throw IllegalArgumentException and include a helpful message if possible. Much more informative than NPE

What he said.
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
William Brogden wrote:I would certainly throw IllegalArgumentException and include a helpful message if possible. Much more informative than NPE - consider the work you would have to do with an NPE to figure out what happened.

To be fair, as long as you're creating and throwing the exception yourself, it's just as easy to put a helpful message on an NPE as on an IAE. Just because the JVM doesn't include such helpful messages on its exceptions, doesn't mean we can't.

Further, as Campbell points out, it's pretty much the standard within all the Sun/Oracle libraries to use an NPE in this case - usually with no useful message, I admit. But again, nothing prevents us from fixing that. And since JDK 7, it's even supported by a nice standard utility class, Objects. (Also useful for making good, readable equals() and hashCode() methods quickly and easily.) However I do slightly disagree with Campbell on one point:

Campbell Ritchie wrote:

The requireNotNull() method does conveniently throw an NPE with a custom message, exactly as we recommend. However, I think the one thing that would be most useful in such a message is: which argument is null, when it shouldn't be? A sharp-eyed coder can work this out from the stack trace, looking at which of the two lines is throwing the exception. But why not make it obvious in the message?

 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Google Guava has helper methods similar to Objects.requireNotNull, in its Preconditions class:
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Anilprem wrote:To me, there is one major issue with throwing NPE and ArrayIndexOutOfBoundsException. These exceptions indicate that the violation has already occurred...

Absolutely. To me, NPE is an effect, not a cause; and for this reason (as William said) they can be notoriously difficult to track down even if you know where they happened.

Therefore, don't muddy the waters by throwing an exception that the JVM already throws itself.

@Roel: And another little tip for you: Don't return nulls from methods unless it's absolutely unavoidable and very well documented.

My 2¢.

Winston
 
Campbell Ritchie
Marshal
Posts: 56581
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike Simmons wrote: . . . it's pretty much the standard within all the Sun/Oracle libraries to use an NPE in this case - usually with no useful message, . . .
Agree with MS. The problem with nulls is that you cannot tell what is null. A programmer can tell what is happening if you write this sort of thing… but it may be impossible for the JVM to do so. The local variable and parameter identifiers are not reflected in the bytecode, so it is often impossible for the JVM to throw an NPE and say “x is null in Foo.java line 12345”

I would enhance those error messages to what they would have been if I had been awake when I wrote the post "b must not be null in mathod Foo#bar".
Maybe you would want to write a utility classMaybe I shall agree with whoever writes, “Who cares what sort of Exception you use, as long as you have a good error message to go with it.”
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Maybe I shall agree with whoever writes, “Who cares what sort of Exception you use, as long as you have a good error message to go with it.”

Again, absolutely. However, in answer to the question in the title, I'd say IllegalArgumentException - for all the reasons Paul mentioned.

<sigh>We disagree again</sigh> - but that's what makes programming fun.

Winston
 
Tim Cooke
Marshal
Posts: 4051
239
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would favour a NPE for a null param, and an IAE for a non-null param that's incorrect for the domain. However, consistency is key so if I were working with a codebase that threw IAE's for null params then I would continue to do so.

As a point of note, Effective Java summarises the occasions for use for commonly reused exceptions:
Effective Java wrote:NullPointerException : Parameter value is null where prohibited.
IllegalArgumentException : Non-null parameter value is inappropriate.

The Google Guava Preconditions library that Jesper mentions follows this convention with its checkNotNull() and checkArgument() functions, which throw NPE and IAE respectively.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!