NullPointerException vs IllegalArgumentException
Niclas Hedhman
niclas at hedhman.org
Thu Jan 6 02:28:21 EST 2005
Gang,
Steve and I have different opinions about what to throw when a null argument
is passed into a method that doesn't accept it.
His style is;
if( abc == null )
throw new NullPointerException( "abc" );
whereas my style is;
if( abc == null )
throw new IllegalArgumentException( "Null argument: abc" );
Instead of tossing this issue back and forth (I will claim that the JDK
javadocs clearly states "Thrown to indicate that a method has been passed an
illegal or inappropriate argument.") , and Steve will say "NPE is preempted
in time" and "xxx throws it in the JDK", I would like to propose that we make
a reasonable compromise;
public class NullArgumentException extends IllegalArgumentException
{
}
and put that into the Util package.
and that the patter is;
if( abc == null )
throw new NullArgumentException( "abc" );
I picked up the pattern from the Geronimo folks, and it made perfect sense to
me. The checks for null arguments are extensive, so I think it validates an
explicit exception, the IllegalArgumentException semantic is honored and can
not be misunderstood.
The switch over can also happen gradually whenever we have time and is of no
urgency whatsoever.
WDYT?
Cheers
Niclas
--
---------------
My mechanic told me, "I couldn't repair your brakes, so I made your horn
louder."
- Steven Wright
+---------//-------------------+
| http://www.dpml.net |
| http://niclas.hedhman.org |
+------//----------------------+
More information about the dev-dpml
mailing list