數夢工廠筆試題回顧一----finally在return之後執行還是之前?,----finallyreturn
1 package test;
2
3 public class TestFinally {
4 public static void main(String[] args) {
5 Demo demo=new Demo();
6 TestFinally tf=new TestFinally();
7 String result=tf.fun1(demo);
8 System.out.println(result);
9 System.out.println(demo.str);
10 }
11 private String fun1(Demo demo) {
12 try {
13 demo.str+="1";
14 return fun2(demo);
15 }finally{
16 demo.str+="2";
17 }
18 }
19 private String fun2(Demo demo) {
20 try {
21 demo.str+="3";
22 return demo.str;
23 }finally{
24 demo.str+="4";
25 }
26 }
27 }
28 class Demo{
29 String str="";
30 }
View Code
執行結果: