@NonNull throwing a more appropriate IllegalArgumentException

1,769 views
Skip to first unread message

Martin Ahrer

unread,
Mar 20, 2014, 6:53:07 AM3/20/14
to project...@googlegroups.com
Is it possible to customize @NonNull to throw a (from my point of view) more appropriate IllegalArgumentException?
I personally think throwing a NPE from a NonNull violation is not the right thing because no null reference is actually dereferenced hence no NPE.

Thanks
Martin

Roel Spilker

unread,
Mar 20, 2014, 10:12:12 AM3/20/14
to project...@googlegroups.com

Effective Java Second Edition by Josh Bloch describes in "Item 60: Favor the use of standard exceptions":

"Argument, all erroneous method invocations boil down to an illegal argument or an illegal state, but other exceptions are standardly used for certain kinds of arguments or states. If a caller passes null in some parameter for which null values are prohibited, convention dictates that  NullPointerException be thrown rather than IllegalArgumentException"

I do recommend everyone to buy a copy of the book, read it at least once from cover to cover and reread it after a few years.

--
You received this message because you are subscribed to the Google Groups "Project Lombok" group.
To unsubscribe from this group and stop receiving emails from it, send an email to project-lombo...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Martin Grajcar

unread,
Mar 20, 2014, 10:29:53 AM3/20/14
to project...@googlegroups.com
See also this holy war question:

I guess in practice it hardly ever matters as I've never seen any code handling NPE and IAE differently except for Guava's NullPointerTester which expects an NPE. See also Guava's issue 1648.

Fabrizio Giudici

unread,
Mar 20, 2014, 1:47:02 PM3/20/14
to project...@googlegroups.com, Martin Grajcar
On Thu, 20 Mar 2014 15:29:53 +0100, Martin Grajcar <maaar...@gmail.com>
wrote:

> See also this holy war question:
> http://stackoverflow.com/questions/3881/illegalargumentexception-or-nullpointerexception-for-a-null-parameter
>
> I guess in practice it hardly ever matters as I've never seen any code
> handling NPE and IAE differently except for Guava's

Yes, it's mostly a formal thing, so I don't expect Lombok will be changed,
even though I hate the use of NPE here for the reasons perfectly stated by
the first response at SO. And I re-read Effective Java multiple times, and
the point on NPE vs IAE was wrong the latest time as it was the first :-)


--
Fabrizio Giudici - Java Architect @ Tidalwave s.a.s.
"We make Java work. Everywhere."
http://tidalwave.it/fabrizio/blog - fabrizio...@tidalwave.it

Reinier Zwitserloot

unread,
Mar 20, 2014, 2:50:19 PM3/20/14
to project-lombok

Effective Java is as close to a standard as we have.

We don't like perpetuating a "there are two ways to do this" situation.

The arguments in that stack overflow thread in favour of using IAE instead of NPE are typical idiotic sky is falling fodder. If only out of principle, this means NPE wins. Example:

" Agree with Thorbjorn, do yourself a favor and avoid debugging hell."

Oh, yup, somebody found a religion here. I'd there a Godwin's for usage of "hell"?

--
You received this message because you are subscribed to the Google Groups "Project Lombok" group.
To unsubscribe from this group and stop receiving emails from it, send an email to project-lombok+unsubscribe@googlegroups.com.

Martin Odhelius

unread,
May 22, 2014, 11:50:14 AM5/22/14
to project...@googlegroups.com
I have a public REST API where I throw IllegalArgumentException when the external users of the API sending in null where it is not supposed to be sent in. I can therefor safely ignore these types of exceptions in my log if they occurs (and yes, they occurs a lot since the REST API have several million users per day ;)). I would not like to filter out NullPointerException from my log though since those can indeed happen accidentally because of null pointer bugs. That is the reason I prefer IllegalArgumentException before NullPointerException.

It is also extremely inconsistent to handle one type of non preferred argument in a completely different way than all other arguments.

When you read the javadocs about these exceptions it is pretty clear that IllegalArgumentException shall be used when "a method has been passed an illegal or inappropriate argument". I think that is much more specific than the much more vague "Applications should throw instances of this class to indicate other illegal uses of the null object."

Effective Java is in fact a fantastic book, but I also think conversions shall not be followed just for the sake of it, especially when the conversion is not well reinforce with good reasons. The only valid reason to use NullPointerException before IllegalArgumentException that I have heard so far is that it is a conversion since it is written in Effective Java. To be honest I think that smells a bit like cargo cult programming.

Anyway, it seems like form lombok 0.14 it would be possible to add lombok config to your project. How about add a config for this, and make people choose? ;)

Best Regards
Martin
To unsubscribe from this group and stop receiving emails from it, send an email to project-lombo...@googlegroups.com.

Reinier Zwitserloot

unread,
May 22, 2014, 12:53:01 PM5/22/14
to project-lombok

Effective Java is the bible we use. This IS a holy war which by definition means there is no provably right answer, we have to pick a side and accept that a large number of people are unhappy no matter which side we pick.

NPE wins because (A) Effective Java says so, and "when in doubt, follow EJ" is a convenient fallback, and (B) the current version of Lombok uses NPE already. No need to rock the boat if the alternative is not better, just different.

We could create a conf key, I guess. I'll talk it over with Roel.

Reinier Zwitserloot

unread,
May 27, 2014, 3:24:12 PM5/27/14
to project...@googlegroups.com
I couldn't find a reasonable argument not to implement this request now that we have the config system, so, have at it: You can now create 'lombok.config' in your project or workspace root and add:

config.stopBubbling = true
lombok.nonNull.exceptionType = IllegalArgumentException

and you'll now get j.l.IllegalArgumentException("fieldName is null") instead of j.l.NullPointerException("fieldName"). You cannot set this behaviour on @NonNull (if you could, that'd be about as much boilerplate as just writing the if statement by hand!).

To unsubscribe from this group and stop receiving emails from it, send an email to project-lombok+unsubscribe@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages