【问题标题】:OpenJDK 14.0.1 gives "the switch expression does not cover all possible input values"OpenJDK 14.0.1 给出“switch 表达式不涵盖所有可能的输入值”
【发布时间】:2020-08-17 06:11:19
【问题描述】:

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

}

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

【问题讨论】:

标签: java enums switch-statement javac


【解决方案1】:

你需要添加默认大小写,像这样:

public class Example {
private String test(final ExampleEnum ee) {
    return switch (ee) {
        case Value -> null;
        default -> throw new IllegalStateException("Unexpected value");
    };
}

【讨论】:

    猜你喜欢
    • 2022-10-07
    • 2023-02-09
    • 2020-05-24
    • 1970-01-01
    • 2013-11-30
    • 2021-12-29
    • 2014-08-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多