return遇到finally
1 public class Test { 2 3 public static void main(String[] args) { 4 System.out.println("=============test1=================="); 5 System.out.println(test1()); 6 System.out.println("==============================="); 7 8 System.out.println("=============test1_1=================="); 9 System.out.println(test1_1()); 10 System.out.println("==============================="); 11 12 System.out.println("\n============test2==================="); 13 System.out.println(test2()); 14 System.out.println("==============================="); 15 16 System.out.println("\n============test2_1==================="); 17 System.out.println(test2_1()); 18 System.out.println("==============================="); 19 20 System.out.println("\n============test3==================="); 21 System.out.println(test3()); 22 System.out.println("==============================="); 23 24 System.out.println("\n============test3_1==================="); 25 System.out.println(test3_1()); 26 System.out.println("==============================="); 27 } 28 29 public static String test1() { 30 String a = "in try"; 31 32 try{ 33 return a; 34 } catch ( Exception e ) { 35 36 } finally { 37 a = "in finally"; 38 System.out.println("do finally"); 39 } 40 41 return a; 42 } 43 44 public static String test1_1() { 45 String a = "in try"; 46 47 try{ 48 return a; 49 } catch ( Exception e ) { 50 51 } finally { 52 a = "in finally"; 53 System.out.println("do finally"); 54 return a; 55 } 56 } 57 58 public static int test2() { 59 int a = 1; 60 61 try{ 62 return a; 63 } catch ( Exception e ) { 64 65 } finally { 66 a = 2; 67 System.out.println("do finally"); 68 } 69 70 return a; 71 } 72 73 public static int test2_1() { 74 int a = 1; 75 76 try{ 77 return a; 78 } catch ( Exception e ) { 79 80 } finally { 81 a = 2; 82 System.out.println("do finally"); 83 return a; 84 } 85 } 86 87 public static Helper test3() { 88 Helper a = new Helper(); 89 a.a = 1; 90 91 try{ 92 return a; 93 } catch ( Exception e ) { 94 95 } finally { 96 a.a = 2; 97 System.out.println("do finally"); 98 } 99 100 return a; 101 } 102 103 public static Helper test3_1() { 104 Helper a = new Helper(); 105 a.a = 1; 106 107 try{ 108 return a; 109 } catch ( Exception e ) { 110 111 } finally { 112 a.a = 2; 113 System.out.println("do finally"); 114 return a; 115 } 116 } 117 118 static class Helper { 119 int a; 120 121 public String toString() { 122 return String.valueOf(a); 123 } 124 } 125 }
运行的结果:
1 =============test1================== 2 do finally 3 in try 4 =============================== 5 =============test1_1================== 6 do finally 7 in finally 8 =============================== 9 10 ============test2=================== 11 do finally 12 1 13 =============================== 14 15 ============test2_1=================== 16 do finally 17 2 18 =============================== 19 20 ============test3=================== 21 do finally 22 2 23 =============================== 24 25 ============test3_1=================== 26 do finally 27 2 28 ===============================
结论:
- 在try catch块里return的时候,finally也会被执行。
- return 语句会把后面的值复制到一份用来返回,如果return的是基本类型的,finally里对变量的改动将不起效果,如果return 的是引用类型的,改动将可以起效果。
- finally里的return语句会把try catch块里的return语句效果给覆盖掉。
看来return语句并不一定都是函数的出口,执行return时,只是把return后面的值复制了一份到返回值变量里去了。看来最佳实践是:
- 最好把return放到方法尾而不要在try cath 里return
- 不要在try catch块和finally块里都包含return
- 如果在try catch块里return, 则不要在finally块里操作被return的变量
文章来自:http://www.360doc.com/content/11/0905/19/1542811_146016871.shtml