IllegalArgumentException vs NullPointerException
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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
)-
2 -
-
Number of slices to send:Optional 'thank-you' note:
-
-
As many beginners have discovered it is possible to track an NPE to a statement and still have no idea what happened.
Bill
-
1 -
-
Number of slices to send:Optional 'thank-you' note:
-
-
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
-
2 -
-
Number of slices to send:Optional 'thank-you' note:
-
-
Enthuware - Best Mock Exams and Questions for Oracle Java Certifications
Quality Guaranteed - Pass or Full Refund!
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
William Brogden wrote:I would certainly throw IllegalArgumentException and include a helpful message if possible. Much more informative than NPE
What he said.
-
2 -
-
Number of slices to send:Optional 'thank-you' note:
-
-
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?
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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”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, . . .
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.”
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.
Tim Driven Development | Test until the fear goes away
| 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 |












