javac allows to catch a checked exception if hidden inside an intersection type

Remi Forax forax at univ-mlv.fr
Fri Jun 18 22:01:35 UTC 2021


Hi all,
this code has two bugs.

  public static void execute(Runnable runnable) throws IOException {
    try {
      runnable.run();
    } catch(RuntimeException | IOException e) {   // oops 1
      var cause = e instanceof UncheckedIOException unchecked ? unchecked.getCause(): e;
      throw cause;  // oops 2
    }
  }

The catch (oops 1) should be rejected because the try block doesn't throw IOException, but javac accepts it.

Intellij does not see any bugs, like javac it does not see "oops 1". It also types "cause" as a RuntimeException | IOException instead of Exception,
the common super type because ?: does not support precise exceptions like a catch/throw does so it should compute the lowest upper bound).

Eclipse correctly reports the two issues.

regards,
Rémi


More information about the compiler-dev mailing list