You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by Ola Berg <ol...@arkitema.se> on 2002/08/21 22:23:17 UTC
[commons]NPE or IAE?
A quickie.
When contributing new code directly to Commons, would you like me to use IllegalArgumentException for all illegal arguments to a method, or NullPointerException for the illegal nulls?
This question goes out to
1) all Commons, since this is fresh code in Commons
2) io, since the particular class is in io
3) lang, since it works closely with lang
Preferences, anybody?
Personally, I prefer IAE, since that leaves me with only one exception to throw for all illegal arguments, regardless of cause. Putting the right value in a parameter is something you do in order to maintain consistency, avoiding NPE and other runtime exceptions. IMO.
Second: there was a thread on coding conventions lately. I wonder if it would be a benefit if questions like these (such as naming conventions: XxxUtils/Xxxs, when to use toXxx and when to use asXxx etc) could be answered in a \"prefered style\" document: a non binding style guide that reflects the most popular styles of the committers, or lists the acceptable styles (sorted in order of preference maybe)?
IIRC, XxxUtils was favored over Xxxs by the majority, but was that documented (elsewhere than in the mails)? In a document where the answer to the NPE/IAE question should go too?
/O
--------------------
ola.berg@arkitema.se
0733 - 99 99 17
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>
Re: [commons]NPE or IAE?
Posted by di...@multitask.com.au.
ola.berg@arkitema.se wrote on 22/08/2002 06:23:17 AM:
> A quickie.
>
> When contributing new code directly to Commons, would you like me to
> use IllegalArgumentException for all illegal arguments to a method,
> or NullPointerException for the illegal nulls?
NPEs for the illegal nulls. From the JDK docs:
Thrown when an application attempts to use null in a case where an object
is required. These include:
Calling the instance method of a null object.
Accessing or modifying the field of a null object.
Taking the length of null as if it were an array.
Accessing or modifying the slots of null as if it were an array.
Throwing null as if it were a Throwable value.
Applications should throw instances of this class to indicate other
illegal uses of the null object.
> Preferences, anybody?
[snip]
--
dIon Gillard, Multitask Consulting
Work: http://www.multitask.com.au
Developers: http://adslgateway.multitask.com.au/developers
Re: [commons]NPE or IAE?
Posted by Henri Yandell <ba...@generationjava.com>.
I do recall this coming up in Collections and I think they chose to throw
NPE if null was passed in and not IAE, I think because Sun stated that for
Collections.
A preferred style document sounds good to me. Possibly one project could
adopt it, [Collections? Lang?] and others would choose to go with it or
not.
I would be tempted to think that Collections would be best placed for this
and am +1 for Lang/Util/IO to adhere any coding standard which Collections
choose.
Hen
On Wed, 21 Aug 2002, Ola Berg wrote:
> A quickie.
>
> When contributing new code directly to Commons, would you like me to use IllegalArgumentException for all illegal arguments to a method, or NullPointerException for the illegal nulls?
>
> This question goes out to
> 1) all Commons, since this is fresh code in Commons
> 2) io, since the particular class is in io
> 3) lang, since it works closely with lang
>
> Preferences, anybody?
>
> Personally, I prefer IAE, since that leaves me with only one exception
to throw for all illegal arguments, regardless of cause. Putting the right
value in a parameter is something you do in order to maintain consistency,
avoiding NPE and other runtime exceptions. IMO.
>
> Second: there was a thread on coding conventions lately. I wonder if
> it would be a benefit if questions like these (such as naming
> conventions: XxxUtils/Xxxs, when to use toXxx and when to use asXxx
> etc) could be answered in a \"prefered style\" document: a non binding
> style guide that reflects the most popular styles of the committers,
> or lists the acceptable styles (sorted in order of preference maybe)?
>
> IIRC, XxxUtils was favored over Xxxs by the majority, but was that documented (elsewhere than in the mails)? In a document where the answer to the NPE/IAE question should go too?
>
> /O
>
> --------------------
> ola.berg@arkitema.se
> 0733 - 99 99 17
>
> --
> To unsubscribe, e-mail: <ma...@jakarta.apache.org>
> For additional commands, e-mail: <ma...@jakarta.apache.org>
>
>
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>
RE: [commons]NPE or IAE?
Posted by Steven Caswell <st...@caswell.name>.
I tend to agree with Michael's observations about using different
exceptions. It has been my experience is that using different exceptions
to indicate different conditions is preferred (and IMHO these are
different conditions). This presents the most flexibility to the
caller.
Steven Caswell
steven@caswell.name
a.k.a Mungo Knotwise of Michel Delving
"One ring to rule them all, one ring to find them..."
> -----Original Message-----
> From: Michael A. Smith [mailto:mas@apache.org]
> Sent: Wednesday, August 21, 2002 3:42 PM
> To: Jakarta Commons Developers List; Ola Berg
> Subject: Re: [commons]NPE or IAE?
>
>
> On Wed, 21 Aug 2002, Ola Berg wrote:
>
> > A quickie.
> >
> > When contributing new code directly to Commons, would you
> like me to
> > use IllegalArgumentException for all illegal arguments to a
> method, or
> > NullPointerException for the illegal nulls?
> >
> > This question goes out to
> > 1) all Commons, since this is fresh code in Commons
> > 2) io, since the particular class is in io
> > 3) lang, since it works closely with lang
> >
> > Preferences, anybody?
>
> my preference is for both. If an argument is not valid (e.g.
> passing in
> a negative number when a positive is expected), throw an
> IllegalArgumentException. If there's a null when an object
> is expecte,d
> throw a NullPointerException.
>
> > Personally, I prefer IAE, since that leaves me with only
> one exception
> > to throw for all illegal arguments, regardless of cause.
> Putting the
> > right value in a parameter is something you do in order to maintain
> > consistency, avoiding NPE and other runtime exceptions. IMO.
>
> But distinguishing the cause can be important. Different
> exceptions for
> different exceptional conditions.
>
> NullPointerException - "Applications should throw instances
> of this class to indicate other illegal uses of the null object."
>
> IllegalArgumentException implies that an argument was supplied and
> illegal (different than not being supplied at all, as in a
> null object):
> "Thrown to indicate that a method has been passed an illegal or
> inappropriate argument."
>
> > Second: there was a thread on coding conventions lately. I
> wonder if
> > it would be a benefit if questions like these (such as naming
> > conventions: XxxUtils/Xxxs, when to use toXxx and when to use asXxx
> > etc) could be answered in a \"prefered style\" document: a
> non binding
> > style guide that reflects the most popular styles of the
> committers,
> > or lists the acceptable styles (sorted in order of
> preference maybe)?
> >
> > IIRC, XxxUtils was favored over Xxxs by the majority, but was that
> > documented (elsewhere than in the mails)? In a document where the
> > answer to the NPE/IAE question should go too?
>
> the XxxUtils over Xxxs was documented for the collections component:
>
http://cvs.apache.org/viewcvs/*checkout*/jakarta-commons/collections/DEV
ELOPERS-GUIDE.html
regards,
michael
--
To unsubscribe, e-mail:
<ma...@jakarta.apache.org>
For additional commands, e-mail:
<ma...@jakarta.apache.org>
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>
RE: [commons]NPE or IAE?
Posted by Steven Caswell <st...@caswell.name>.
I think this is a good starting point. +1
Steven Caswell
steven@caswell.name
a.k.a Mungo Knotwise of Michel Delving
"One ring to rule them all, one ring to find them..."
> -----Original Message-----
> From: Henri Yandell [mailto:bayard@generationjava.com]
> Sent: Wednesday, August 21, 2002 3:55 PM
> To: Jakarta Commons Developers List
> Cc: Ola Berg
> Subject: Re: [commons]NPE or IAE?
>
>
>
> >
> http://cvs.apache.org/viewcvs/*checkout*/jakarta-commons/collections/D
> > EVELOPERS-GUIDE.html
>
> What do you think to the idea of pulling a coding style out
> of this document that other projects could declare their obedience to?
>
> [And extending it with some other things. asXxxx for
> decorator names etc, or if that's wrong, whatever Ola suggested.] Hen
>
>
> --
> To unsubscribe, e-mail:
> <mailto:commons-dev-> unsubscribe@jakarta.apache.org>
> For
> additional commands,
> e-mail: <ma...@jakarta.apache.org>
>
>
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>
Re: [commons]NPE or IAE?
Posted by Henri Yandell <ba...@generationjava.com>.
> http://cvs.apache.org/viewcvs/*checkout*/jakarta-commons/collections/DEVELOPERS-GUIDE.html
What do you think to the idea of pulling a coding style out of this
document that other projects could declare their obedience to?
[And extending it with some other things. asXxxx for decorator names etc,
or if that's wrong, whatever Ola suggested.]
Hen
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>
Re: [commons]NPE or IAE?
Posted by "Michael A. Smith" <ma...@apache.org>.
On Wed, 21 Aug 2002, Ola Berg wrote:
> A quickie.
>
> When contributing new code directly to Commons, would you like me to
> use IllegalArgumentException for all illegal arguments to a method, or
> NullPointerException for the illegal nulls?
>
> This question goes out to
> 1) all Commons, since this is fresh code in Commons
> 2) io, since the particular class is in io
> 3) lang, since it works closely with lang
>
> Preferences, anybody?
my preference is for both. If an argument is not valid (e.g. passing in
a negative number when a positive is expected), throw an
IllegalArgumentException. If there's a null when an object is expecte,d
throw a NullPointerException.
> Personally, I prefer IAE, since that leaves me with only one exception
> to throw for all illegal arguments, regardless of cause. Putting the
> right value in a parameter is something you do in order to maintain
> consistency, avoiding NPE and other runtime exceptions. IMO.
But distinguishing the cause can be important. Different exceptions for
different exceptional conditions.
NullPointerException - "Applications should throw instances of this
class to indicate other illegal uses of the null object."
IllegalArgumentException implies that an argument was supplied and
illegal (different than not being supplied at all, as in a null object):
"Thrown to indicate that a method has been passed an illegal or
inappropriate argument."
> Second: there was a thread on coding conventions lately. I wonder if
> it would be a benefit if questions like these (such as naming
> conventions: XxxUtils/Xxxs, when to use toXxx and when to use asXxx
> etc) could be answered in a \"prefered style\" document: a non binding
> style guide that reflects the most popular styles of the committers,
> or lists the acceptable styles (sorted in order of preference maybe)?
>
> IIRC, XxxUtils was favored over Xxxs by the majority, but was that
> documented (elsewhere than in the mails)? In a document where the
> answer to the NPE/IAE question should go too?
the XxxUtils over Xxxs was documented for the collections component:
http://cvs.apache.org/viewcvs/*checkout*/jakarta-commons/collections/DEVELOPERS-GUIDE.html
regards,
michael
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>