public class Main {
public static void main(String[] args) {
System.out.print(fun1());
}
public static String fun1() {
try {
System.out.print("A");
return fun2();
} finally {
System.out.print("B");
}
}
public static String fun2() {
System.out.print("C");
return "D";
}
}
//输出ACBD
//先计算return的值再执行finally再return
本文详细解析了Java中finally块在方法返回时的执行顺序,通过具体代码示例说明了如何在finally块中进行资源清理,并确保在返回前正确执行。强调了在finally块中执行的代码将始终在方法返回前运行,即使在try或catch块中有return语句。
311

被折叠的 条评论
为什么被折叠?



