instanceof and exceptions

Remi Forax forax at univ-mlv.fr
Fri Oct 9 10:11:48 UTC 2020


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 ?

Rémi


More information about the amber-spec-observers mailing list