directory-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jérôme Baumgarten <jbaumgar...@gmail.com>
Subject Re: [OT] How best to respond to null arguments?
Date Thu, 01 Sep 2005 09:18:49 GMT
On 8/31/05, Alex Karasulu <aok123@bellsouth.net> 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

I would definitely avoid.

> Option #2: Test to see if arg is null early and throw a descriptive NPE

I usually go for that one and as you propose with a descriptive message.

> Option #3: Test to see if arg is null early and throw a descriptive IAE

I go for that one when the arg is not null but not well-formed. For
example a DN class :
public void DN(String dn) {
  if (dn == null) throw new NullPointerException("A DN cannot be null");
  if (! isValid(dn)) throw new IllegalArgumentException("The DN " + dn
+ " is not valid.");
  //...
}
> 
> Which sounds best?
> 
> Alex
> 
>

Mime
View raw message