OpenJDK 14.0.1给出“开关表达式未涵盖所有可能的输入值”

问题描述 投票:7回答:1

使用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 {
    }

}

编译失败,“开关表达式未涵盖所有可能的输入值”。如果我从InnerEnum中删除ExampleEnum,则会编译代码。为什么此内部枚举的存在会导致switch表达式失败?有逻辑解释还是编译器错误?

java enums switch-statement javac
1个回答
0
投票

您需要添加默认大小写,如下所示:

public class Example {
private String test(final ExampleEnum ee) {
    return switch (ee) {
        case Value -> null;
        default -> throw new IllegalStateException("Unexpected value");
    };
}
© www.soinside.com 2019 - 2024. All rights reserved.