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


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

Re: can't throw

Newsgroups comp.lang.java.programmer
Date 2012-09-12 23:15 -0700
References <19af6b94-7a1e-4491-afb2-79782406f560@googlegroups.com> <k2o8sm$ve3$1@dont-email.me> <fea53dab-0659-4f9f-af44-c181c4508aa1@googlegroups.com> <504fe3a6$0$293$14726298@news.sunsite.dk> <505109cf$0$284$14726298@news.sunsite.dk>
Message-ID <af44cf50-2495-42c6-89f6-ba52c0d60791@googlegroups.com> (permalink)
Subject Re: can't throw
From Lew <lewbloch@gmail.com>

Show all headers | View raw


Arne Vajhøj wrote:
> Arne Vajhøj wrote:
>> Lew wrote:
>>> Eric Sosman wrote:
>>>>       Some people, deeper thinkers than I, consider the whole
>>>> business of checked exceptions a nuisance or a misfeature.  The
> 
>>> Those people are mistaken.
> 
>>>> need for a dodge like the above can be seen as evidence for
>>>> that viewpoint, but I don't find it overwhelmingly convincing.
> 
>>> Not even suggestive, much less convincing.
> 
>>>> In any event, that's Java As It Is And Is Likely To Remain, so
> 
>>> and for good reason.
> 
>> I must admit that personally I like the checked exception
>> context.
> 
> It makes a lot of sense if you are API centric.
> 
> A contract "I will either return a value of type X or throw
> one of the exceptions E1,...,En" seems a lot more strict than
> "I will either return a value of type X or throw an exception
> but I will not tell you which".

Agreed, although some chafe at the restriction, and point out 
that other code works just fine without such things.

In some cases that's because the platform implicitly supports 
some exception-handling mechanism that wraps more than what 
Java's does, or the language uses some assertion-like mechanism 
to render exceptions moot.

Java's checked exceptions serve a real but optional need, not 
only to be strict, but to warn API users (yes, this is an 
API-centric or API-ontologic view) of expected exceptions.

Otherwise all exceptions are RuntimeExceptions or worse, and 
by definition come as a surprise.

By convention and just the way it works out, RuntimeExceptions 
are for program(mer) errors. The infamous NPE is an example. 
Most use cases are better served by explicit null guards than 
exception tosses.

Not to say that RuntimeExceptions aren't useful; they are. 
The suggestions here to wrap checked exceptions in them
are an example. That's because an exception visible to one 
thread from another are a surprise, and most likely a 
program(mer) error. *Especially* because checked exceptions 
are by definition expected and should usually not propagate 
outside their thread.

Into logs, yes. As exceptions, not.

Throwing 'Exception' itself is a Dark Art. You can play 
with poison - there are valid reasons to do so.

'Error' and raw 'Throwable' - whatever the interpretation here, 
it's a bad-ass event.

Like all programming idioms, when you write exceptions 
you should have a good flair for their purpose. Arne makes 
sense - balances a case for their utility with a recognition 
of both their limits and that people do without them.

As he says overthread, "One needs to think about the issue anyway."
Exceptions provide one way to express the decisions you make on 
how to handle it.

A key for me is an idea that some code path is exceptional, 
and another is a "happy" path. Another key is that I pair 
(not) throwing an exception with an assertion.

Example:

  public void foo(Bar param)
  {
    if (param == null || param.getBaz() != State.READY)
    {
      throw new IllegalStateException("Bad param "+param);
    }
    assert param != null && param.getBaz() == State.READY;
 . . .
  }

-- 
Lew
All rules of thumb are useful as far as they go. But no farther.
As a rule.

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


Thread

can't throw bob smith <bob@coolfone.comze.com> - 2012-09-11 13:16 -0700
  Re: can't throw markspace <-@.> - 2012-09-11 13:34 -0700
    Re: can't throw Lew <lewbloch@gmail.com> - 2012-09-11 14:15 -0700
  Re: can't throw Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-09-11 17:02 -0400
    Re: can't throw Lew <lewbloch@gmail.com> - 2012-09-11 14:17 -0700
      Re: can't throw Arne Vajhøj <arne@vajhoej.dk> - 2012-09-11 21:21 -0400
        Re: can't throw Peter Duniho <NpOeStPeAdM@NnOwSlPiAnMk.com> - 2012-09-11 21:59 -0700
          Re: can't throw Gene Wirchenko <genew@ocis.net> - 2012-09-12 09:18 -0700
            Re: can't throw Robert Klemme <shortcutter@googlemail.com> - 2012-09-12 19:09 +0200
            Re: can't throw markspace <-@.> - 2012-09-12 10:56 -0700
            Re: can't throw Arne Vajhøj <arne@vajhoej.dk> - 2012-09-12 18:36 -0400
              Re: can't throw Gene Wirchenko <genew@ocis.net> - 2012-09-12 18:29 -0700
                Re: can't throw Patricia Shanahan <pats@acm.org> - 2012-09-12 18:34 -0700
                Re: can't throw Arne Vajhøj <arne@vajhoej.dk> - 2012-09-12 21:37 -0400
                Re: can't throw Arne Vajhøj <arne@vajhoej.dk> - 2012-09-12 21:54 -0400
          Re: can't throw Jim Janney <jjanney@shell.xmission.com> - 2012-09-12 11:27 -0600
          Re: can't throw Arne Vajhøj <arne@vajhoej.dk> - 2012-09-12 18:24 -0400
            Re: can't throw Peter Duniho <NpOeStPeAdM@NnOwSlPiAnMk.com> - 2012-09-13 22:20 -0700
              Re: can't throw Gene Wirchenko <genew@ocis.net> - 2012-09-14 09:49 -0700
                Re: can't throw Robert Klemme <shortcutter@googlemail.com> - 2012-09-14 20:50 +0200
                Re: can't throw Lew <lewbloch@gmail.com> - 2012-09-14 13:02 -0700
                Re: can't throw Gene Wirchenko <genew@ocis.net> - 2012-09-14 14:06 -0700
                Re: can't throw Gene Wirchenko <genew@ocis.net> - 2012-09-14 13:16 -0700
                Re: can't throw Robert Klemme <shortcutter@googlemail.com> - 2012-09-14 23:07 +0200
                Re: can't throw Lew <lewbloch@gmail.com> - 2012-09-14 14:28 -0700
                Re: can't throw Gene Wirchenko <genew@ocis.net> - 2012-09-16 20:04 -0700
        Re: can't throw Arne Vajhøj <arne@vajhoej.dk> - 2012-09-12 18:16 -0400
          Re: can't throw Lew <lewbloch@gmail.com> - 2012-09-12 23:15 -0700
        Re: can't throw Leif Roar Moldskred <leifm@dimnakorr.com> - 2012-09-14 17:33 -0500
          Re: can't throw Robert Klemme <shortcutter@googlemail.com> - 2012-09-16 15:46 +0200
            Re: can't throw Leif Roar Moldskred <leifm@dimnakorr.com> - 2012-09-16 12:17 -0500
              Re: can't throw Robert Klemme <shortcutter@googlemail.com> - 2012-09-16 22:36 +0200
                Re: can't throw Gene Wirchenko <genew@ocis.net> - 2012-09-16 20:07 -0700
                Re: can't throw Robert Klemme <shortcutter@googlemail.com> - 2012-09-17 07:41 +0200
                Re: can't throw Gene Wirchenko <genew@ocis.net> - 2012-09-17 09:51 -0700
                Re: can't throw Robert Klemme <shortcutter@googlemail.com> - 2012-09-17 21:00 +0200
                Re: can't throw Gene Wirchenko <genew@ocis.net> - 2012-09-17 13:23 -0700
                Re: can't throw Joerg Meier <joergmmeier@arcor.de> - 2012-09-17 18:52 +0200
                Re: can't throw Lew <lewbloch@gmail.com> - 2012-09-17 11:22 -0700
                Re: can't throw Leif Roar Moldskred <leifm@dimnakorr.com> - 2012-09-17 02:52 -0500
                Re: can't throw Leif Roar Moldskred <leifm@dimnakorr.com> - 2012-09-17 02:39 -0500
                Re: can't throw Robert Klemme <shortcutter@googlemail.com> - 2012-09-17 21:02 +0200
  Re: can't throw markspace <-@.> - 2012-09-11 14:28 -0700
  Re: can't throw Arne Vajhøj <arne@vajhoej.dk> - 2012-09-11 21:14 -0400
    Re: can't throw Arne Vajhøj <arne@vajhoej.dk> - 2012-09-12 18:11 -0400
  Re: can't throw Robert Klemme <shortcutter@googlemail.com> - 2012-09-12 08:31 +0200
    Re: can't throw bob smith <bob@coolfone.comze.com> - 2012-09-12 11:40 -0700
      Re: can't throw Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-09-12 15:50 -0400
      Re: can't throw Lew <lewbloch@gmail.com> - 2012-09-12 12:52 -0700
        Re: can't throw Robert Klemme <shortcutter@googlemail.com> - 2012-09-12 23:24 +0200
          Re: can't throw Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2012-09-12 15:10 -0700
            Re: can't throw Robert Klemme <shortcutter@googlemail.com> - 2012-09-13 23:32 +0200
    Re: can't throw Arne Vajhøj <arne@vajhoej.dk> - 2012-09-12 18:06 -0400
  Re: can't throw Roedy Green <see_website@mindprod.com.invalid> - 2012-09-12 20:55 -0700

csiph-web