Multi-catch and exhaustiveness check
Thiago Henrique Hupner
thihup at gmail.com
Mon Aug 1 12:28:12 UTC 2022
Hello all!
I've played around with the exhaustiveness check and I'd like to
discuss whether the following code should compile or it is right not to
compile.
import java.util.*;
import java.io.*;
sealed abstract class MyAbstractException extends RuntimeException {
final static class MyException extends MyAbstractException {}
}
final class MyUncheckedException extends UncheckedIOException {
public MyUncheckedException() {
super(null);
}
}
public class SealedException {
public static void main(String[] args) {
try {
throw new MyUncheckedException();
} catch (MyAbstractException | MyUncheckedException e) {
switch (e) {
case MyAbstractException.MyException a -> {}
case MyUncheckedException b -> {}
}
}
}
}
As MyUncheckedExceptionh is final, and MyAbstractException.MyException is
the only implementation available for MyAbstractException, I guess it is
exhaustive, but the compiler disagrees.
SealedException.java:23: error: the switch statement does not cover all
possible input values
switch (e) {
^
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/amber-dev/attachments/20220801/d43a84d3/attachment-0001.htm>
More information about the amber-dev
mailing list