commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Steve Downey <steve.dow...@netfolio.com>
Subject Re: [lang] StringUtils.containsOnly
Date Thu, 26 Sep 2002 18:55:39 GMT
On Thursday 26 September 2002 01:23 pm, Berin Loritsch wrote:
> Stephen Colebourne wrote:
> > I go further than this - in my world a product like [lang] would never
> > throw NPEs. If it did then that would be a bug. If null is a disallowed
> > parameter then that should be checked and thrown as an
> > IllegalArgumentException. But thats just my coding style.
> >
> > Its probably a pity that we didn't agree a uniform approach to this, but
> > its too late now. The priority is to document what we have correctly.
> >
> > Stephen
>
> I have to agree with you on both of your points Stephen.  NPE's are
> indicative of something terribly wrong.  To me it is the Java equivalent
> of the C++ pointer error.  At least with an IllegalArgumentException,
> you know you skrewed up using the method.  An NPE could mean anything
> from an illegal argument (see, IllegalArgumentException) to the method
> is just completely foo-barred.  I reserve NPEs for things that are
> foo-barred and IllegalArgumentExceptions for wrong arguments.

Well, I agree with you, Berin, and disagree with Stephen, I think. 

As I understand it, Stephen is arguing that StringUtils should never throw a 
runtime exception when presented with null. I'm arguing that it should. The 
runtime exception that java.lang.String throws in similar situations is NPE. 

IllegalArgumentException is starting to be adopted by other parts of the API 
for null arguments, eg JAXP, however. It was originally used for exceptions 
like undefined locale, or where an int is used as an enum and the argument 
wasn't one of the defined enums.

If it is a case of arg == null, that can make sense. However, checking every 
object before access for being == to null, and converting that to a different 
exception leads to messy code. 

I don't like treating null as "", which is the common 'solution' to avoiding 
NPEs. For example, in StringUtils.center(String str, int size), which embeds 
str in the center of a string of length size, right now you get an NPE if you 
call StringUtils.center(null, 80). You shouldn't ever call the method with 
null. It's not meaningful. I wouldn't like to see it return a string with 80 
spaces.


--
To unsubscribe, e-mail:   <mailto:commons-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:commons-dev-help@jakarta.apache.org>


Mime
View raw message