在下面的代码中,我想了解return
语句的行为。Finally
block 将始终执行,因此该函数将始终返回30
,但是try block 中的return语句有什么意义。try block 中的return是否向其调用者返回值,调用者会将这个值存储在某个堆栈中,并在finally
block 中看到return语句时替换?或者 try block 中的 return 会调用 finally
block ?
public static int test() {
try {
return 10;// to whom this 10 is returned to?
} catch (Exception e) {
return 20;
}finally{
System.out.println("Finally");
return 30;
}
}
提前致谢。
最佳答案
10
和 30
都会返回。但是 30
位于堆栈
的顶部(最上面的元素)。所以调用者得到的返回值为30
。
关于java - try catch finally 中 return 的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22068289/