有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

没有编译器错误的java多返回语句

这是一个采访问题:

public class Demo {

    public static void main(String[] args) {
        System.out.println(foo());
    }

    static String foo() {
        try {
            return "try ...";
        } catch (Exception e) {
            return "catch ...";
        } finally {
            return "finally ..."; //got as result
        }
    }
}

我的问题是为什么没有编译时错误。当我的finally块中有return语句时,它将从finally而不是trycatch块返回。我试图用-Xlint选项编译这段代码,它给出了如下警告

warning: [finally] finally clause cannot complete normally

共 (0) 个答案