Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > NullPointerException, IllegalArgumentException, or AssertionErrorfor null constructor argument

Reply
Thread Tools

NullPointerException, IllegalArgumentException, or AssertionErrorfor null constructor argument

 
 
Daniel Pitts
Guest
Posts: n/a
 
      12-28-2007
I have a constructor that takes a String argument. I'd like to throw an
exception if the constructor is invoked with a null argument, but I'm
not sure which instruction I should use.
NullPointerException is technically accurate, since it is a null
pointer, but it is also an IllegalArgumentException. I think that
IllegalArgumentException is more specific, so I'll probably go with
that, but wanted opinions.

The third option is AssertionError. I could just use assert arg!=null,
and that could be enough. This is for a personal project, so it doesn't
*really* matter, but at the same time its good practice for me to
think about these sort of things

Thoughts?

Thanks,
Daniel.
--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
 
Reply With Quote
 
 
 
 
Owen Jacobson
Guest
Posts: n/a
 
      12-28-2007
On Dec 28, 2:11*pm, Daniel Pitts
<(E-Mail Removed)> wrote:
> I have a constructor that takes a String argument. I'd like to throw an
> exception if the constructor is invoked with a null argument, but I'm
> not sure which instruction I should use.
> NullPointerException is technically accurate, since it is a null
> pointer, but it is also an IllegalArgumentException. *I think that
> IllegalArgumentException is more specific, so I'll probably go with
> that, but wanted opinions.
>
> The third option is AssertionError. *I could just use assert arg!=null,
> and that could be enough. *This is for a personal project, so it doesn't
> * *really* matter, but at the same time its good practice for me to
> think about these sort of things
>
> Thoughts?


Thoughts: private members get assertions if anything at all for
parameter checking (as well as other invariants); public members get
explicit tests if it matters that parameters meet specific
requirements. I tend to leave NPE to the java runtime to throw and
use IllegalArgumentException for things caught in explicit tests.

Asserts can, after all, be turned off.

Throwing AssertionError explicitly seems like the Wrong Thing.
 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      12-28-2007
Daniel Pitts wrote:
> I have a constructor that takes a String argument. I'd like to throw an
> exception if the constructor is invoked with a null argument, but I'm
> not sure which instruction I should use.
> NullPointerException is technically accurate, since it is a null
> pointer, but it is also an IllegalArgumentException. I think that
> IllegalArgumentException is more specific, so I'll probably go with
> that, but wanted opinions.
>
> The third option is AssertionError. I could just use assert arg!=null,
> and that could be enough. This is for a personal project, so it doesn't
> *really* matter, but at the same time its good practice for me to think
> about these sort of things


IllegalArgumentException would get my vote, with a possible
last-minute in-the-voting-booth defection to NullPointerException.
AssertionError is the candidate whose placards I'd tear down, no
matter what the election authorities might think.

--
Eric Sosman
http://www.velocityreviews.com/forums/(E-Mail Removed)lid
 
Reply With Quote
 
Patricia Shanahan
Guest
Posts: n/a
 
      12-28-2007
Eric Sosman wrote:
> Daniel Pitts wrote:
>> I have a constructor that takes a String argument. I'd like to throw
>> an exception if the constructor is invoked with a null argument, but
>> I'm not sure which instruction I should use.
>> NullPointerException is technically accurate, since it is a null
>> pointer, but it is also an IllegalArgumentException. I think that
>> IllegalArgumentException is more specific, so I'll probably go with
>> that, but wanted opinions.
>>
>> The third option is AssertionError. I could just use assert
>> arg!=null, and that could be enough. This is for a personal project,
>> so it doesn't *really* matter, but at the same time its good practice
>> for me to think about these sort of things

>
> IllegalArgumentException would get my vote, with a possible
> last-minute in-the-voting-booth defection to NullPointerException.
> AssertionError is the candidate whose placards I'd tear down, no
> matter what the election authorities might think.
>


Agree with this, with the possible addition of a merger between the two
acceptable options - throw an IllegalArgumentException with a
NullPointerException as cause. However, IllegalArgumentException with a
clear message should normally do the job.

Patricia
 
Reply With Quote
 
Arne Vajh°j
Guest
Posts: n/a
 
      12-29-2007
Daniel Pitts wrote:
> I have a constructor that takes a String argument. I'd like to throw an
> exception if the constructor is invoked with a null argument, but I'm
> not sure which instruction I should use.
> NullPointerException is technically accurate, since it is a null
> pointer, but it is also an IllegalArgumentException. I think that
> IllegalArgumentException is more specific, so I'll probably go with
> that, but wanted opinions.


IllegalArgumentException

Because

NullPointerException : tells me that the code called
a member on the argument without checking it first

IllegalArgumentException : tells me that code checked
the argument first

and the second one is the correct.

Arne
 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      12-29-2007
Owen Jacobson wrote:
> On Dec 28, 2:11 pm, Daniel Pitts
> <(E-Mail Removed)> wrote:
>> I have a constructor that takes a String argument. I'd like to throw an
>> exception if the constructor is invoked with a null argument, but I'm
>> not sure which instruction I should use.
>> NullPointerException is technically accurate, since it is a null
>> pointer, but it is also an IllegalArgumentException. I think that
>> IllegalArgumentException is more specific, so I'll probably go with
>> that, but wanted opinions.
>>
>> The third option is AssertionError. I could just use assert arg!=null,
>> and that could be enough. This is for a personal project, so it doesn't
>> *really* matter, but at the same time its good practice for me to
>> think about these sort of things
>>
>> Thoughts?

>
> Thoughts: private members get assertions if anything at all for
> parameter checking (as well as other invariants); public members get
> explicit tests if it matters that parameters meet specific
> requirements. I tend to leave NPE to the java runtime to throw and
> use IllegalArgumentException for things caught in explicit tests.
>
> Asserts can, after all, be turned off.
>
> Throwing AssertionError explicitly seems like the Wrong Thing.


It is. It is completely contrary to the purpose of the 'assert' mechanism,
and it's an Error, not an Exception. The 'assert' mechanism is not meant for
runtime checks of data, but for test-time checks of algorithmic correctness.

The great third phase of programming I don't see mentioned much is test-time.
Everyone talks about compile-time and run-time, but 'assert' is a leader in
test-time functionality.

Again, its purpose is to check the algorithm, not the data.

--
Lew
 
Reply With Quote
 
tzvika.barenholz@gmail.com
Guest
Posts: n/a
 
      12-29-2007
On Dec 29, 12:11 am, Daniel Pitts
<(E-Mail Removed)> wrote:
> I have a constructor that takes a String argument. I'd like to throw an
> exception if the constructor is invoked with a null argument, but I'm
> not sure which instruction I should use.
> NullPointerException is technically accurate, since it is a null
> pointer, but it is also an IllegalArgumentException. I think that
> IllegalArgumentException is more specific, so I'll probably go with
> that, but wanted opinions.
>
> The third option is AssertionError. I could just use assert arg!=null,
> and that could be enough. This is for a personal project, so it doesn't
> *really* matter, but at the same time its good practice for me to
> think about these sort of things
>
> Thoughts?
>
> Thanks,
> Daniel.
> --
> Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>


I vote NPE. It's more specific, and basically it's what it's there
fore. IAE could mean any number of things.
Of course if you decide on NPE, you don't actually have to check in
the constructor and explicitly throw NPE - just let it be thrown on
method calls. This is more general, in that it allows construction
with null, then calling setXXX with a legal value, which is useful for
some complex objects.
T
 
Reply With Quote
 
Patricia Shanahan
Guest
Posts: n/a
 
      12-29-2007
(E-Mail Removed) wrote:
> On Dec 29, 12:11 am, Daniel Pitts
> <(E-Mail Removed)> wrote:
>> I have a constructor that takes a String argument. I'd like to throw an
>> exception if the constructor is invoked with a null argument, but I'm
>> not sure which instruction I should use.
>> NullPointerException is technically accurate, since it is a null
>> pointer, but it is also an IllegalArgumentException. I think that
>> IllegalArgumentException is more specific, so I'll probably go with
>> that, but wanted opinions.
>>
>> The third option is AssertionError. I could just use assert arg!=null,
>> and that could be enough. This is for a personal project, so it doesn't
>> *really* matter, but at the same time its good practice for me to
>> think about these sort of things
>>
>> Thoughts?
>>
>> Thanks,
>> Daniel.
>> --
>> Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>

>
> I vote NPE. It's more specific, and basically it's what it's there
> fore. IAE could mean any number of things.


IAE means that an argument was invalid, and the Javadoc comments should
give the rules.

Even if the Javadocs indicate that NPE can be caused by a null argument,
short of reading the code, the caller has no way of knowing if the NPE
is due to that, or due to some other problem in the called code. There
are far too many things that cause NPE.

If you are worried about explaining what was wrong with the argument,
you could report the NPE as cause on the IAE. In any case, the message
should be clear.

Patricia
 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      12-29-2007
(E-Mail Removed) wrote:
> Of course if you decide on NPE, you don't actually have to check in
> the constructor and explicitly throw NPE - just let it be thrown on
> method calls. This is more general, in that it allows construction
> with null, then calling setXXX with a legal value, which is useful for
> some complex objects.


Bad advice.

If construction with null is allowed, then it should never throw an NPE,
inside the constructor or passed through to the caller. If you need special
handling for null, use
if ( foo == null )
not an NPE, to check for it.

Certainly the constructor shouldn't pass through any exceptions without a
logging discipline. (Nor should any method.)

Checked exceptions are a good way to ensure that client code will handle the
exception thrown by a constructor or method.

--
Lew
 
Reply With Quote
 
Mark Rafn
Guest
Posts: n/a
 
      12-29-2007
>On Dec 29, 12:11 am, Daniel Pitts
>> I have a constructor that takes a String argument. I'd like to throw an
>> exception if the constructor is invoked with a null argument

....

(E-Mail Removed) <(E-Mail Removed)> wrote:
>I vote NPE. It's more specific, and basically it's what it's there


I vote IllegalArgumentException. NPE implies a bug or unexpected state inside
the code, where IAE makes it clear that the bug is in the code making the
call.

AssertionError is right out. Assertions exist to make low-severity bugs into
high-severity ones so you can find and fix them. They're not part of an API
design.
--
Mark Rafn (E-Mail Removed) <http://www.dagon.net/>
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
A constructor calling another constructor (default constructor)? Generic Usenet Account C++ 10 11-28-2007 04:12 AM
"stringObj == null" vs "stringObj.equals(null)", for null check?? qazmlp1209@rediffmail.com Java 5 03-29-2006 10:37 PM
How to pass variable argument list to another function w/ variable argument list? Ben Kial C Programming 1 11-15-2004 01:51 AM
call null constructor from another constructor Matt Graham C++ 3 02-07-2004 08:41 AM
defaulting argument to previous argument Bhushit Joshipura C++ 5 12-30-2003 03:21 PM



Advertisments