switch中没有大小写匹配时如何抛出异常

bla*_*awk -1 java android switch-statement

我有一个具有 switch 块的函数,当没有匹配项时我想抛出异常,我该怎么做,我写了这个开关

private static int ClimateCal() {
    int climateCal = 0;
    switch (climate.toLowerCase()){
        case "Hot":
            climateCal = 0;
            break;
        case "Cold":
            climateCal = 250;
            break;
        case "Moderate":
            climateCal = 500;
            break;
        default:
            Exception exception;
    }
    return climateCal;
}
Run Code Online (Sandbox Code Playgroud)

Abb*_*bas 5

尝试

private static int ClimateCal() throws Exception {         // Add throws keyword here If you want to catch the exception by the calling method.
    int climateCal = 0;
    switch (climate.toLowerCase()){
        case "Hot":
            climateCal = 0;
            break;
        case "Cold":
            climateCal = 250;
            break;
        case "Moderate":
            climateCal = 500;
            break;
        default:
            throw new Exception();
    }
    return climateCal;
}
Run Code Online (Sandbox Code Playgroud)