I think the answer depends on the role of the class.
If it's a client, or a "top level" manager, it's a good place to
validate, rather than passing on crap information to further regions of
code - IllegalArgumentExceptions are good for this (option #3)
If it's a low level piece of code which isn't in charge of validating
information it shouldn't have to think about it at all. (option #1).
That's my 2 cents.
Cheers,
Nick
Alex Karasulu wrote:
Hi all,
I was playing again with my very many split personalities when I
realized one likes to throw NullPointerExceptions after checking if an
argument is null while another likes to throw IllegalArgumentExceptions
since null is value the argument can take. Yet another personality
does not care and figures a NPE will result anyway when the null
argument is referenced so why bother.
Question: How best to respond to null arguments?
Option #1: Ignore it and let a NPE result on its own
Option #2: Test to see if arg is null early and throw a descriptive NPE
Option #3: Test to see if arg is null early and throw a descriptive IAE
Which sounds best?
Alex
--
ATLASSIAN - http://www.atlassian.com/
Confluence - the enterprise wiki - tried it yet?
http://www.atlassian.com/confluence/
--