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:[EMAIL PROTECTED]> For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>
