Why does Precondition#checkNotNull(..) throws an NPE and not IllegalArgEx ?

2,380 views
Skip to first unread message

Christian Schwarz

unread,
Nov 9, 2012, 9:32:14 AM11/9/12
to guava-...@googlegroups.com
Why does Precondition#checkNotNull(..) throws an NullPointerException and not an IllegalArgumentException if null is passed ? I must admit that this question is a matter of taste. The first time i used this method, it feels like an small anti-pattern, but i  did not know why. Now i do, there are 2 reasons:
  1. Normally it don't throw NPE in my Code, only the VM does when i access a null reference/pointer
  2. All other checkXXX-methods throw an IllegalArgumentException,  checkNotNull(..) has an asymetric behaviour
Wouldn't it be more consequent to throw an IllegalArgumentException if an precondition fail? I think an NPE should only be thrown by the VM not by Code, in the most of all cases an other exception type can describe a failure better. An NPE is the result of an unchecked  precondition.


What do you think? 

P.S. A change from NPE to IllegalArgEx would be break existing client code i guess, so a change is not very likely here.

Robert Konigsberg

unread,
Nov 9, 2012, 9:37:04 AM11/9/12
to Christian Schwarz, guava-discuss
If you prefer IAEs, use Preconditions.checkArgument.





--
Robert Konigsberg

Chris Povirk

unread,
Nov 9, 2012, 9:44:11 AM11/9/12
to Christian Schwarz, guava-...@googlegroups.com
This is indeed controversial:
http://stackoverflow.com/questions/3881/illegalargumentexception-or-nullpointerexception-for-a-null-parameter

You're right that we're stuck, for better or for worse, with the
decision that we've made.

(Note that it's not quite true that all other checkXXX methods throw
IAE: That's only true of checkArgument (and, in rare cases,
checkXXXIndex).)

A few small advantages to NPE:

1) Checking happens automatically in many cases, so there's less code to write.

2) A small number of APIs (notably AbstractMap.equals) requires that
certain operations thrown NPE and not IAE. This is a more important
rule to us than to the average developer because of the number of
collections we write.

3) If NullPointerTester accepted IAE, it would mask bugs when an IAE
is thrown for some other reason (since NPT doesn't necessarily know
what are valid values for the other parameters):

Foo(String s, Iterable it) {
checkArgument(s.length() > 0);
// fail to check that it isn't null
}

Kevin Bourrillion

unread,
Nov 9, 2012, 9:47:53 AM11/9/12
to Chris Povirk, Christian Schwarz, guava-...@googlegroups.com
On Fri, Nov 9, 2012 at 6:44 AM, Chris Povirk <cpo...@google.com> wrote:

Foo(String s, Iterable it) {
  checkArgument(s.length() > 0);
  // fail to check that it isn't null
}

This puzzled me until I realized it means:

// fail to check that 'it' isn't null

:-)


--
Kevin Bourrillion | Java Librarian | Google, Inc. | kev...@google.com

Louis Wasserman

unread,
Nov 9, 2012, 12:33:22 PM11/9/12
to Kevin Bourrillion, Chris Povirk, Christian Schwarz, guava-...@googlegroups.com
I have to admit I consider NPE absolutely the right exception to throw here, and it's code that throws IAEs on null that should be considered abnormal.




--
Louis Wasserman

Paul Bellora

unread,
Nov 9, 2012, 5:10:13 PM11/9/12
to Louis Wasserman, Kevin Bourrillion, Chris Povirk, Christian Schwarz, guava-...@googlegroups.com
If I can just chime in to agree with Louis, I tend to see checkNotNull as a means to fail fast and always while still throwing the same exception that may have been thrown by the JVM otherwise. There are places in the core API that already did such a thing manually.
Reply all
Reply to author
Forward
0 new messages