On Thursday 01 September 2005 02:23, Alex Karasulu wrote:
> Question: How best to respond to null arguments?
A subject every programmer will have an opinion about, I guess.
> 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
if( arg == null )
{
throw new IllegalArgumentException( "arg == null " );
}
is my personal choice from your list. Alternatively Option #4;
if( arg == null )
{
throw new NullArgumentException( "arg" );
}
public class NullArgumentException extends IllegalArgumentException
{}
I think consensus can be reached at "Do nothing is not good" for any API
methods, and that consistency within the codebase should be strived for.
Everything else is probably bikeshedding.
Cheers
Niclas