try、catch、finally相关问题

try、catch、finally相关问题



(原文地址:http://blog.csdn.net/Dove_Knowledge/article/details/71077512



问题描述:

try{}里有一个return语句,那么紧跟在这个try{}后面的finally{}中的代码是否会被执行?

如果会的话,什么时候被执行,在return之前还是return之后?


在Java语言的异常处理中,finally块的作用就是为了保证无论出现什么情况,finally块里的代码一定会被执行。

由于程序执行return就意味着结束对当前函数的调用并跳出这个函数体,因此任何语句要执行都只能在return前执行(除非碰到exit函数),因此finally块里的代码也是在return之前执行的

此外,如果try-finally或者catch-finally中都有return,那么finally块中的return将会覆盖别处的return语句,最终返回到调用者那里的是finally中return的值。


下面通过一个例子来说明这个问题:

[java]  view plain  copy
  1. package com.js;  
  2. /** 
  3.  * try-catch中有return语句,finally中代码运行时机问题 
  4.  * @author jiangshuai 
  5.  */  
  6.   
  7. public class Test{  
  8.     public static int testFinally(){  
  9.         try {  
  10.             return 1;  
  11.         } catch (Exception e) {  
  12.             return 0;  
  13.         }finally{  
  14.             System.out.println("execute finally");  
  15.         }  
  16.     }  
  17.     public static void main(String[] args){  
  18.         int result = testFinally();  
  19.         System.out.println(result);  
  20.     }  
  21. }  
运行结果:

execute finally
1


从上面这个例子中可以看出,在执行return语句前确实执行了finally块中的代码。紧接着,在finally块里放置个return语句,来看看到底最终返回的是哪个return语句的值。


例子如下图所示:

[java]  view plain  copy
  1. package com.js;  
  2. /** 
  3.  * try-catch中有多个return语句,研究return的是哪一个 
  4.  * @author jiangshuai 
  5.  */  
  6.   
  7. public class Test{  
  8.     public static int testFinally(){  
  9.         try {  
  10.             return 1;  
  11.         } catch (Exception e) {  
  12.             return 0;  
  13.         }finally{  
  14.             System.out.println("execute finally");  
  15.             return 3;  
  16.         }  
  17.     }  
  18.     public static void main(String[] args){  
  19.         int result = testFinally();  
  20.         System.out.println(result);  
  21.     }  
  22. }  
运行结果:

execute finally
3


从以上运行结果可以看出,当finally块中有return语句时,将会覆盖函数中其他return语句。

此外,由于在一个方法内部定义的变量都存储在栈中,当这个函数结束后,其对应的栈就会被回收,此时在其方法体中定义的变量将不存在了,因此,对基本类型的数据,在finally块中改变return的值对返回值没有任何影响,而对引用类型的数据会有影响


下面通过一个例子来说明这个问题:

[java]  view plain  copy
  1. package com.js;  
  2. /** 
  3.  * 在finally块中改变基本数据类型、引用类型对比 
  4.  * @author jiangshuai 
  5.  */  
  6.   
  7. public class Test{  
  8.     public static int testFinally1(){  
  9.         int result = 1;  
  10.         try {  
  11.             result = 2;  
  12.             return result;  
  13.         } catch (Exception e) {  
  14.             return 0;  
  15.         }finally{  
  16.             result = 3;  
  17.             System.out.println("execute finally1");  
  18.         }  
  19.     }  
  20.     public static StringBuffer testFinally2(){  
  21.         StringBuffer s = new StringBuffer("Hello");  
  22.         try {  
  23.             return s;  
  24.         } catch (Exception e) {  
  25.             return null;  
  26.         }finally{  
  27.             s.append(" World");  
  28.             System.out.println("execute finally2");  
  29.         }  
  30.     }  
  31.     public static void main(String[] args){  
  32.         int result = testFinally1();  
  33.         System.out.println(result);  
  34.         StringBuffer resultRef = testFinally2();  
  35.         System.out.println(resultRef);  
  36.     }  
  37. }  
运行结果:

execute finally1
2
execute finally2
Hello World


程序在执行到return时会首先将返回值存储在一个指定的位置,其次去执行finally块,最后再返回。

在方法testFinally1中调用return前,先把result的值1存储在一个指定的位置,然后再去执行finally块中的代码,此时修改result的值将不会影响到程序的返回结果。

在方法testFinally2中,在调用return前先把s存储到一个指定的位置,由于s为引用类型,因此在finally中修改s将会修改程序的返回结果。



出现在Java程序中的finally块是不是一定会被执行?

答案:不一定


下面给出两个finally块不会被执行的例子:

1)、当程序进入try块之前就出现异常时,会直接结束,不会执行finally块中的代码,示例如下:

[java]  view plain  copy
  1. package com.js;  
  2. /** 
  3.  * 在try之前发生异常 
  4.  * @author jiangshuai 
  5.  */  
  6.   
  7. public class Test{  
  8.     public static void testFinally1(){  
  9.         int result = 1/0;  
  10.         try {  
  11.             System.out.println("try block");  
  12.         } catch (Exception e) {  
  13.             System.out.println("catch block");  
  14.         }finally{  
  15.             System.out.println("finally block");  
  16.         }  
  17.     }  
  18.     public static void main(String[] args){  
  19.         testFinally1();  
  20.     }  
  21. }  
运行结果:

Exception in thread "main" java.lang.ArithmeticException: / by zero
at com.js.Test.testFinally1(Test.java:9)
at com.js.Test.main(Test.java:19)
程序在执行1/0时会抛出异常,导致没有执行try块,因此finally块也就不会被执行。


2)、当程序在try块中强制退出时也不会去执行finally块中的代码,示例如下:

[java]  view plain  copy
  1. package com.js;  
  2. /** 
  3.  * 在try之前发生异常 
  4.  * @author jiangshuai 
  5.  */  
  6.   
  7. public class Test{  
  8.     public static void testFinally1(){  
  9.         try {  
  10.             System.out.println("try block");  
  11.             System.exit(0);  
  12.         } catch (Exception e) {  
  13.             System.out.println("catch block");  
  14.         }finally{  
  15.             System.out.println("finally block");  
  16.         }  
  17.     }  
  18.     public static void main(String[] args){  
  19.         testFinally1();  
  20.     }  
  21. }  
运行结果:

try block


上例在try块中通过调用System.exit(0)强制退出了程序,因此导致finally块中的代码没有被执行。







猜你喜欢

转载自blog.csdn.net/qq_35888875/article/details/78731824