When---什么时候需要finally:
在jdk1.7之前,所有涉及到I/O的相关操作,我们都会用到finally,以保证流在最后的正常关闭。jdk1.7之后,虽然所有实现Closable接口的流,可以通过在try块中定义,从而实现jvm自动关闭输入输出流。但其实在我们需要在代码块返回之前,实现在不管前面的操作是否执行成功,都要执行的某操作A。这时候我们就可以将A放入finally块中。很常见的一个操作就是锁的unlock操作。
What---什么是finally:
字面解释就是最终,eventually。其作用就是保证在try块中的代码执行完成之后,必然会执行finally中的语句。不管try块中是否抛出异常。
How---如何使用finally:
finally块必须配合try块使用,而不能单独使用。
Deeper---深入探究finally块:
在深入探究之前我们直接先给出四个结论:
1. finally块只会在try块执行的情况下才执行
2. finally块在离开try块执行完成后或者try块未执行完成但是接下来是控制转移语句时(return/continue/break)在控制转移语句之前执行。
3. 在执行finally语句之前,控制转移语句会将返回值存在本地变量中
4. finally块中的控制转移return语句能够覆盖try或者catch块中的返回值
其中1、2不做进一步说明。我们来对3和4直接通过两个例子进行深入理解:
关于3的例子:
1 public static void main(String[] args) {
2 System.out.println("return " + testFinally());
3 }
4
5 public static String testFinally() {
6 int i = 10;
7 try {
8 return i + "";
9 } finally {
10 i++; // 即使i的值改变,但是return语句涉及到的i的值已经保存在本地变量中
11 }
12 }
13
14 // 输出是10.
我们直接分析字节码得:
1 public static java.lang.String testFinally();
2 descriptor: ()Ljava/lang/String;
3 flags: ACC_PUBLIC, ACC_STATIC
4 Code:
5 stack=3, locals=3, args_size=0
6 0: bipush 10 --将10压入栈中
7 2: istore_0 --将栈顶元素(10)存储到局部变量0中
8 3: new #22 // class java/lang/StringBuilder
9 6: dup
10 7: iload_0 --从局部变量0中装载数据(10)入栈,后续代码是用来进行字符串拼接
11 8: invokestatic #47 // Method java/lang/String.valueOf:(I)Ljava/lang/String;
12 11: invokespecial #26 // Method java/lang/StringBuilder."<init>":(Ljava/lang/String;)V
13 14: invokevirtual #37 // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
14 17: astore_2 --拼接后的结果存入局部变量2中(是10+"")
15 18: iinc 0, 1 --局部变量0的值加1
16 21: aload_2 --从局部变量2中装载数据(为10+"")
17 22: areturn --返回结果
18 23: astore_1 --将前面变量0加1的结果存入局部变量1
19 24: iinc 0, 1 --将局部变量0加1
20 27: aload_1 --装载局部变量1的值
21 28: athrow
其实areturn返回的是变量的引用值。这意味着,如果最终返回的是对象类型。那么在finally块执行之前,存储在本地变量中的只是对象引用的值,而不是对象的深拷贝。我们可以在finally块中对返回对象的属性进行改变。直接看下一个例子:
1 public static void main(String[] args) {
2 System.out.println("return " + testFinally());
3 }
4
5 public static Map<String, String> testFinally() {
6 Map<String, String> map = new HashMap<String, String>();
7 try {
8 map.put("aaa", "aaa");
9 return map;
10 } finally {
11 map.put("aaa", "bbb");
12 map = null;
13 }
14 }
15
16 // 输出为:return {aaa=bbb}
下面我们再来说说关于第4点的例子:
1 public static void main(String[] args) {
2 System.out.println("return " + testFinally());
3 }
4
5 private static String testFinally() {
6 try {
7 return "456";
8 } finally {
9 return "123";
10 }
11 }
12
13 // 输出:123
1 public static void main(String[] args) {
2 System.out.println("return " + testFinally());
3 }
4
5 private static String testFinally() {
6 try {
7 throw new SQLException();
8 //return "456";
9 } catch (SQLException e) {
10 System.out.println("catch SQLException!");
11 throw new RuntimeException("123");
12 }
13 finally {
14 return "123";
15 }
16 }
17
18 // 输出为:
19 catch SQLException!
20 return 123
更多的其他例子:
1 public static int getValue() {
2 int i = 1;
3 try {
4 i = 4;
5 } finally {
6 i++;
7 return i;
8 }
9 }
10 // 最终返回5
11
12 public static int getValue() {
13 int i = 1;
14 try {
15 i = 4;
16 } finally {
17 i++;
18 }
19
20 return i;
21 }
22 // 最终返回5
23
24 public static void main(String[] args) {
25 System.out.println(test());
26 }
27 public static String test() {
28 try {
29 System.out.println("try block");
30 return test1();
31 } finally {
32 System.out.println("finally block");
33 }
34 }
35 public static String test1() {
36 System.out.println("return statement");
37 return "after return";
38 }
39 // 最终输出:
40 try block
41 return statement
42 finally block
43 after return
参考链接:
https://www.ibm.com/developerworks/cn/java/j-lo-finally/
http://www.cnblogs.com/lanxuezaipiao/p/3440471.html
http://blog.csdn.net/michealmc/article/details/52237639