try/catch、finally语句的执行顺序测试

public class TestException {

public int get() {
try {
return 1;
} catch (Exception e) {
e.printStackTrace();
} finally { // finally里的都会执行,在try里的return
// 1后还会执行吗?答案经检测:是的。2把1给覆盖了。get()最终返回的是2!
return 2;
}
}

public static void main(String[] args) {
TestException te = new TestException();
System.out.println(te.get());
}

}


问运行结果多少?
结果运行下是2!
try/catch、finally语句的执行顺序测试 - 飞雪安能住酒中 - 飞雪安能住酒中的博客
 原因在程序的注释里已经说明了。
原文地址:https://www.cnblogs.com/hangaozu/p/7544467.html