"case catch" instead of "case throws"?
Patrick Doyle
p.r.doyle at gmail.com
Mon Feb 19 18:09:45 UTC 2024
Fair enough. I do see the grammatical appeal of "case throws" and agree
that it reads better.
I might characterize your answer more as "yes, this syntax is not in line
with the rest of the language, and that's how we want it". :-) In the rest
of Java, "throw" and "throws" indicate where exceptions are emitted, and
"catch" indicates where they are handled (as regrettable as the imperative
nature of the word "catch" might be). This would be the first time where
"throws" introduces a syntax construct that does not throw the exception in
question.
--
Patrick Doyle
p.r.doyle at gmail.com
On Mon, Feb 19, 2024 at 11:45 AM Brian Goetz <brian.goetz at oracle.com> wrote:
> No :)
>
> It was discussed why not to use bare "catch" on the EG list. "Case catch"
> is slightly different, but I'd like to explain why this is not in the
> spirit of the feature.
>
> To recap one of the reasons we rejected bare `catch`: because it is asking
> a question _about the selector_ (did evaluating the selector throw), not
> about the _block_ (did something in this switch block throw.) If we called
> it catch, this would surely be confusing.
>
> The essence of switch is "evaluate a thing, then do one of the following
> things as a result." With an exhaustive switch with no fall through, its
> "exactly one thing"; let's go with that as our model: evaluate a thing,
> then do exactly one of the following things.
>
> We pick which thing to do based on case selectors. Currently we have case
> selectors for constants, patterns, and catch-all (default). Each of these
> selectors should refer back _to the computation_ that is the switch
> selector. A `case <constant>` says "did it evaluate to this constant." A
> `case <pattern>` says "did it evaluate to something that matches this
> pattern." A `case throws <pattern>` says "did it throw something that
> matches this pattern." But "case catch" would be asking "did it evaluate
> to something that catches this exception", which doesn't really even make
> sense.
>
> Further, I know we are all used to the word "catch" from try-catch, but
> "catch" is a very imperative framing, which comes from a day where language
> control constructs were all statements (while, for, if, try, etc), not
> expressions. Over the last 30 years we've learned that statements suck.
> (Because, expressions compose, and statements don't, and composition is the
> best tool we have.)
>
> So I think the desire to hold onto "catch" here (in addition to the
> problems discussed on the EG list) is mostly skating to where the puck
> *was*, rather than where it is *going*.
>
>
>
> On 2/19/2024 9:18 AM, Patrick Doyle wrote:
>
> In Brian's "Uniform handling of failure in switch", he proposes the syntax
> "case throws". I see the motivation (it certainly reads nicely) but since
> such clauses are catching exceptions, wouldn't it be more in line with the
> rest of the language to use "case catch"?
>
> This would also serve to preserve the word "throws" for its usual purpose
> if ever "switch ... throws" becomes a thing.
>
> --
> Patrick Doyle
> p.r.doyle at gmail.com
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/amber-dev/attachments/20240219/5b350206/attachment.htm>
More information about the amber-dev
mailing list