• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Devaka Cooray
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
Bartenders:

IllegalArgumentException vs NullPointerException

 
Sheriff
Posts: 11606
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 )
 
Author and all-around good cowpoke
Posts: 13078
6
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Marshal
Posts: 81617
593
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Ranch Hand
Posts: 789
Python C++ Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
npe is more specific in your example than illegal argument which is an "argument" for throwing npe
 
Enthuware Software Support
Posts: 4928
61
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 67759
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 5250
85
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?

 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Google Guava has helper methods similar to Objects.requireNotNull, in its Preconditions class:
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 81617
593
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 6212
502
IntelliJ IDE Python TypeScript Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
No prison can hold Chairface Chippendale. And on a totally different topic ... my stuff:
The new gardening playing cards kickstarter is now live!
https://www.kickstarter.com/projects/paulwheaton/garden-cards
reply
    Bookmark Topic Watch Topic
  • New Topic