geronimo-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Berin Loritsch <>
Subject Re: [Error handling] NullPointer or IllegalArgument?
Date Thu, 14 Aug 2003 13:19:53 GMT
Alex Blewitt wrote:

> On Thursday, Aug 14, 2003, at 11:36 Europe/London, Jason Dillon wrote:
>> I do not agree with the Exceptions section listed in the wiki.  I 
>> think that a NPE can be thrown where appropriate.  For argument 
>> methods which must not be null a NPE is not appropriate.  I think we 
>> should change the text to make it clear that if you must throw an NPE, 
>> then it is okay, but that you need to have a reason todo so and that 
>> it must have a description.
> I put that there to wean people away from the NPE re: discussions on 
> this topic.
> If you can give me a clear example of when throwing an NPE is desirable, 
> please do so. But for every example you'll give, I'll ask why it 
> couldn't be either an IllegalArgumentException (if it is to do with 
> arguments), or if not a generic RuntimeException (with suitable messages).

(Runs away screaming) NOOOOO!!!!

NEVER throw a generic RuntimeException--it tells you nothing.  I am currently
maintaining a program that uses nothing but RuntimeExceptions, and debugging
the bugger is a serious pain in the ars.

An exception should give you enough information to help you narrow down where
the bug originates.  Too many times the RuntimeException is caught far too late
to do anything useful with it.

A typed exception works much better as you can make more specific actions if
necessary.  I am not saying to never extend a runtime exception, but I am
saying don't use it directly.  IMNSHO RuntimeException and Exception should
be abstract base classes and not concrete at all.

> That way, NPEs can be reserved for unwanted program bugs, and fixed and 
> stamped on with all ferociousness.

If you fail early--NPE or IAE, then you can avoid most unexpected bugs.  I
used to be on the only IAE bandwagon, but then I realised that as long as
you can properly catch and determine what the error is, then NPE and IAE
really don't matter.  Both are errors that should never appear in logs.

With an NPE, which is the *real* error in the following:

void testMethod( Foo bar )

If bar is a required argument (by the implementation it is), then if "bar"
is null, the real exception is that the argument is null.  The JVM will
throw the exception at bar.doSomething() which is not the real error.

Something like that can be driven home better when the bar.doSomething()
method is burried deep inside a long method.  Or worse, if the method is
refactored and the call happens in a completely different method.

The ones that desparately need guards and fail-fast semantics are the
methods that are publically accessible.  Private methods can live without
the guards because they should have been caught ahead of time.

Whether it comes as an NPE or IAE doesn't really matter.  Just as long
as we fail early.


"They that give up essential liberty to obtain a little temporary safety
  deserve neither liberty nor safety."
                 - Benjamin Franklin

View raw message