Return-Path: Delivered-To: apmail-jakarta-commons-dev-archive@apache.org Received: (qmail 78802 invoked from network); 29 Jun 2002 21:42:57 -0000 Received: from unknown (HELO nagoya.betaversion.org) (192.18.49.131) by 209.66.108.5 with SMTP; 29 Jun 2002 21:42:57 -0000 Received: (qmail 27377 invoked by uid 97); 29 Jun 2002 21:43:10 -0000 Delivered-To: qmlist-jakarta-archive-commons-dev@jakarta.apache.org Received: (qmail 27361 invoked by uid 97); 29 Jun 2002 21:43:09 -0000 Mailing-List: contact commons-dev-help@jakarta.apache.org; run by ezmlm Precedence: bulk List-Unsubscribe: List-Subscribe: List-Help: List-Post: List-Id: "Jakarta Commons Developers List" Reply-To: "Jakarta Commons Developers List" Delivered-To: mailing list commons-dev@jakarta.apache.org Received: (qmail 27341 invoked by uid 98); 29 Jun 2002 21:43:09 -0000 X-Antivirus: nagoya (v4198 created Apr 24 2002) X-Authentication-Warning: champion.sslsecure.com: iammichael owned process doing -bs Date: Sat, 29 Jun 2002 16:42:58 -0500 (CDT) From: "Michael A. Smith" X-X-Sender: iammichael@champion.sslsecure.com To: Jakarta Commons Developers List Subject: Re: [lang] NullArgumentException? In-Reply-To: <007f01c21faf$13ef8f80$ad1a29d9@oemcomputer> Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII X-Spam-Rating: 209.66.108.5 1.6.2 0/1000/N X-Spam-Rating: 209.66.108.5 1.6.2 0/1000/N On Sat, 29 Jun 2002, Stephen Colebourne wrote: > From: "Michael A. Smith" > > On Sat, 29 Jun 2002, Stephen Colebourne wrote: > > > I would like to add a NullArgumentException class to the lang.exceptions > > > package. > > > > > > It would be a subclass of IllegalArgumentException. > > > It would be thrown when null is passed in as an argument when it > shouldn't > > > be. > > > > > > Any objections? > > > > That's what java.lang.NullPointerException is for. "Applications should > > throw instances of this class to indicate other illegal uses of the null > > object." > > I have to reasons for not wanting to use NullPointerException. > 1) NullPointerException is an unplanned exception. Its thrown by the runtime > automatically, with no control over the error message. Overloading its use > for specifically validated exceptions is not good. if(foo == null) { throw new NullPointerException("foo was null"); } works fine for me. I don't see why that's not good. If it wasn't don't you think they would have left off the constructor that took a String for the message? > 2) The Collection API doesn't allow NullPointerException to be thrown for a > rejection of null entry into a collection. It states it must be > IllegalArgumentException. Collection class documentation: "Attempting to add an ineligible element throws an unchecked exception, typically NullPointerException or ClassCastException." Collection.add(Object) documentation: "NullPointerException - if the specified element is null and this collection does not support null elements." > In both cases NullArgumentException would make life clearer. I disagree. It makes distinguishing between two different types of exceptional conditions a bit more difficult. There is frequently a difference between passing in a Null and passing in an illegal argument and thus two different exceptions should be used (i.e. they shouldn't inherit from each other). If you only want to catch the IllegalArgumentException case and let a null based exception propogate up the stack, it's not pretty if you have NullArgumentException extend IllegalArgumentException. You'd have to catch the exception, test with instanceof and rethrow). regards, michael -- To unsubscribe, e-mail: For additional commands, e-mail: