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

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


false alarm :(
correction,
i'm now not able to reproduce "oops 1" with javac anymore.

so it's just a bug (oops 2) in IntelliJ.

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

Rémi

----- Mail original -----
> De: "Remi Forax" <forax at univ-mlv.fr>
> À: "compiler-dev" <compiler-dev at openjdk.java.net>
> Envoyé: Samedi 19 Juin 2021 00:01:35
> Objet: javac allows to catch a checked exception if hidden inside an intersection type

> 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