Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #12879

Re: Exception Handling

From Novice <novice@example..com>
Newsgroups comp.lang.java.programmer
Subject Re: Exception Handling
Date 2012-03-11 22:02 +0000
Organization Your Company
Message-ID <XnsA013B773176E8jpnasty@94.75.214.39> (permalink)
References <XnsA012D42584994jpnasty@94.75.214.39> <jjh39f$crc$1@news.albasani.net> <XnsA01385245AD09jpnasty@94.75.214.39> <jjipli$np7$1@news.albasani.net> <Dv77r.36154$zD5.32316@newsfe12.iad>

Show all headers | View raw


Arved Sandstrom <asandstrom3minus1@eastlink.ca> wrote in
news:Dv77r.36154$zD5.32316@newsfe12.iad: 

> On 12-03-11 03:07 PM, Lew wrote:
>> Novice wrote:
> [ SNIP ]
> 
>>> Assuming I'm not, I'm inclined to use the first approach you
>>> suggested. Or perhaps use the technique but throw a
>>> NullPointerException. Bloch implied that IllegalArgumentException
>>> was fine for bad values but that NullPointerException was preferred
>>> where a parameter had a null value. 
>> 
>> That's his opinion.
>> 
>> Many others think that 'IllegalArgumentException' makes more sense if
>> it's an argument that has an illegal value.
>> 
>> What do you think?
> [ SNIP ]
> 
> StackOverflow has a good thread on this:
> http://stackoverflow.com/questions/3881/illegalargumentexception-or-nul
> lpointerexception-for-a-null-parameter. 
> 
> Main takeaway is, in this case as in many others, as Lew suggests, you
> do better to understand *why* people are making certain suggestions,
> and then you can make your own choices. Most things in programming
> aren't black and white.
> 
> Bear in mind, Bloch in "Effective Java" is trying to provide advice.
> He's pretty good at it, but he's not infallible. Sometimes it shades a
> bit too much over into "I say so...". I find it particularly rich that
> he tells us that NPE is to be preferred when a parameter is null and
> it's not allowed to be null, and the main reason is "convention
> dictates": seeing as how he wrote a lot of JDK classes he had a big
> hand in establishing the "convention", so this is quite circular.
> 
Excellent, excellent point ;-) As someone who still feels like a novice 
about a lot of Java, I'm inclined to ascribe omniscience to the gurus and 
I need to stop doing that. They put their pants on one leg at a time just 
like I do and they are capable of being biased or even flat out wrong, 
although I'm sure I'm wrong a LOT more than they are ;-)

> Don't get me wrong: ignore Bloch or Goetz or Lea and other people like
> that at your peril. But if you're experienced with technology X, and
> have in fact been using it as long and probably as thoroughly as some
> pundit, and you run across a statement by said pundit that you just
> can't see the sense of, don't assume the other guy is right.
>
Excellent advice. And of course you've just restated Lew's advice in your 
own words.

> FWIW I use IllegalArgumentException in this case myself. I can't help
> NPEs that are thrown by JDK classes even when I think they made a
> mistake in choosing that exception, but for me an NPE means a
> dereferencing failure, and if I've got the choice to create or
> rethrow, I'll use IllegalArgumentException.
> 
Like I said, I'm easy with either approach. Perhaps I'll use 
IllegalArgumentException on even-numbered days and NullPointerException 
on odd-numbered days just to show how even-handed I am ;-)

-- 
Novice

Back to comp.lang.java.programmer | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

Exception Handling Novice <novice@example..com> - 2012-03-11 01:51 +0000
  Re: Exception Handling Lew <noone@lewscanon.com> - 2012-03-10 18:39 -0800
    Re: Exception Handling Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2012-03-11 11:53 -0300
      Re: Exception Handling Lew <noone@lewscanon.com> - 2012-03-11 10:51 -0700
        Re: Exception Handling Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2012-03-11 16:35 -0300
    Re: Exception Handling Novice <novice@example..com> - 2012-03-11 17:05 +0000
      Re: Exception Handling Lew <noone@lewscanon.com> - 2012-03-11 10:53 -0700
        Re: Exception Handling Novice <novice@example..com> - 2012-03-11 20:36 +0000
      Re: Exception Handling Lew <noone@lewscanon.com> - 2012-03-11 11:07 -0700
        Re: Exception Handling Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2012-03-11 17:00 -0300
          Re: Exception Handling Novice <novice@example..com> - 2012-03-11 22:02 +0000
        Re: Exception Handling Arivald <NOSPAMarivald@interia.pl> - 2012-03-11 21:03 +0100
          Re: Exception Handling Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2012-03-11 18:31 -0300
        Re: Exception Handling Novice <novice@example..com> - 2012-03-11 21:54 +0000
          Re: Exception Handling Patricia Shanahan <pats@acm.org> - 2012-03-11 15:26 -0700
            Re: Exception Handling Novice <novice@example..com> - 2012-03-11 23:23 +0000
              Re: Exception Handling Lew <noone@lewscanon.com> - 2012-03-11 16:52 -0700
                Re: Exception Handling Novice <novice@example..com> - 2012-03-12 17:16 +0000
                Re: Exception Handling Lew <noone@lewscanon.com> - 2012-03-13 08:31 -0700
              Re: Exception Handling Patricia Shanahan <pats@acm.org> - 2012-03-11 17:51 -0700
                Re: Exception Handling Novice <novice@example..com> - 2012-03-12 17:26 +0000
            Re: Exception Handling Arne Vajhøj <arne@vajhoej.dk> - 2012-03-12 14:49 -0400
          Re: Exception Handling Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2012-03-11 20:46 -0300
            Re: Exception Handling Novice <novice@example..com> - 2012-03-12 17:43 +0000
      Re: Exception Handling Patricia Shanahan <pats@acm.org> - 2012-03-11 11:14 -0700
        Re: Exception Handling Novice <novice@example..com> - 2012-03-11 22:35 +0000
          Re: Exception Handling Lew <noone@lewscanon.com> - 2012-03-11 16:58 -0700
            Re: Exception Handling Novice <novice@example..com> - 2012-03-12 15:44 +0000
              Re: Exception Handling Gene Wirchenko <genew@ocis.net> - 2012-03-12 10:34 -0700
      Re: Exception Handling Arivald <NOSPAMarivald@interia.pl> - 2012-03-11 20:34 +0100
        Re: Exception Handling Novice <novice@example..com> - 2012-03-11 22:36 +0000
    Re: Exception Handling Arne Vajhøj <arne@vajhoej.dk> - 2012-03-12 14:41 -0400
  Re: Exception Handling Arne Vajhøj <arne@vajhoej.dk> - 2012-03-12 14:37 -0400
    Re: Exception Handling Novice <novice@example..com> - 2012-03-12 22:43 +0000
      Re: Exception Handling Lew <lewbloch@gmail.com> - 2012-03-12 16:11 -0700
      Re: Exception Handling Arivald <NOSPAMarivald@interia.pl> - 2012-03-13 00:54 +0100
        Re: Exception Handling Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2012-03-13 06:05 -0300

csiph-web