geronimo-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Berin Loritsch <blorit...@apache.org>
Subject Re: [Error handling] NullPointer or IllegalArgument?
Date Thu, 14 Aug 2003 14:26:30 GMT
Alex Blewitt wrote:

<snip/>

> 
>> 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.
> 
> 
> There may well be an argument for that.
> 
> In any case, I wasn't advocating 'Use RuntimeExceptions for all'. I was 
> saying that there's never a reason to want to use NPE, and if you really 
> need to throw an unchecked exception (for example, the interface doesn't 
> allow it) then use a RuntimeException instead.
> 
> Correct me if I misunderstood your post, but it seemed that you assumed 
> I was advocating RuntimeExceptions for everthing. I wasn't. 
> RuntimeExceptions are pretty ugly and should be avoided where possible 
> over another implementation or a checked exception (preferably).
> 
> What I was saying is that NPE is an exception a programmer should never 
> throw, and if checked exceptions or other suitable exceptions (IAE, NAE) 
> don't allow, then throw RuntimeException instead of NPE.
> 


:)

Actually, I would much rather see an NPE with a message (you can include
the name of the parameter in the message) than a generic RuntimeException.

The only time an NPE gives you insufficient information is when it is
automatically generated--there is no message to clue you in.

An NPE communicates that something was null that should not have been, and
any supplied message (which I strongly advocate) would give you the hint as
to what exactly is wrong.

An NPE thrown at the beginning of a method is far easier to test and debug
than one thrown by the JVM.  You know that code sent a null parameter that
wasn't supposed to be sent, so all you have to do is go one step back in
the stack trace to see where the culprit really is.

If you are adamately opposed to NPEs, then IAE (IllegalArgumentExceptions)
are a nice alternative.  Either IMO are far better than generic
RuntimeExceptions.

-- 

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


Mime
View raw message