Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenJDK 14.0.1 gives "the switch expression does not cover all possible input values"

Using OpenJDK 14.0.1

public class Example {
    private String test(final ExampleEnum ee) {
        return switch (ee) {
            case Value -> null;
        };
    }
}
public enum ExampleEnum {

    Value;

    public enum InnerEnum {
    }

}

Compilation fails with "the switch expression does not cover all possible input values". If I remove InnerEnum from ExampleEnum the code compiles. Why does the presence of this inner enum cause the switch expression to fail? Is there a logical explanation or a compiler bug?

like image 449
Matthias Avatar asked May 02 '20 18:05

Matthias


1 Answers

You need add default case, like this:

public class Example {
private String test(final ExampleEnum ee) {
    return switch (ee) {
        case Value -> null;
        default -> throw new IllegalStateException("Unexpected value");
    };
}
like image 73
niushuai Avatar answered Nov 05 '22 07:11

niushuai