instanceof and exceptions

Gavin Bierman gavin.bierman at oracle.com
Fri Oct 9 10:31:59 UTC 2020



> On 9 Oct 2020, at 11:11, Remi Forax <forax at univ-mlv.fr> wrote:
> 
> Following the course on exceptions, where i explain that a catch() is an instanceof,
> two different students ask me why catch() can use '|' in between the exception types but instanceof can not.
> 
> i.e why this code works
>  try {
>    ...
>  } catch(RuntimeException | Error e) {
>    throw e;
>  } catch(Throwable t) {
>    ...
>  }
> 
> but this one doesn't
>  try {
>    ...
>  } catch(Throwable t) {
>    if (t instanceof RuntimeException | Error e) {
>      throw e;
>    }
>    ...
>  }
> 
> I wonder if people will want to do pattern matching on exceptions ?

I’m sure they will! (We mentioned adding pattern matching to catch in our overview document https://cr.openjdk.java.net/~briangoetz/amber/pattern-match.html <https://cr.openjdk.java.net/~briangoetz/amber/pattern-match.html>) The issue - as always - is how we reconcile the syntactic choices that have already been made, and the ones that are natural for our more general setting of pattern matching.

Gavin
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.java.net/pipermail/amber-spec-experts/attachments/20201009/d2065cce/attachment.htm>


More information about the amber-spec-experts mailing list