您当前的位置: 首页 > 

小志的博客

暂无认证

  • 0浏览

    0关注

    1217博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

try-catch-finally-return问题详解

小志的博客 发布时间:2017-04-11 23:46:00 ,浏览量:0

try-catch-finally-return执行问题:

(1)try里有一个return语句,那么紧跟在这个try后的finally里的代码会不会被执行,什么时候执行,在return前还是后;

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. //Java try-catch-finally-return执行问题  
  2. public class Test1  
  3. {  
  4.     public static void main(String[] args)  
  5.     {  
  6.   
  7.         System.out.println(returnTest());  
  8.     }  
  9.   
  10.     public static boolean returnTest()  
  11.     {  
  12.         try  
  13.         {  
  14.             System.out.println("try块");  
  15.             return true;  
  16.         }   
  17.         finally  
  18.         {  
  19.             System.out.println("finally块");  
  20.         }  
  21.     }  
  22. }  
执行结果:
[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. try块  
  2. finally块  
  3. true  

(2)如果finally也有return代码 ,那执行结果是?

代码2:

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. //Java try-catch-finally-return执行问题  
  2. public class Test1  
  3. {  
  4.     public static void main(String[] args)  
  5.     {  
  6.   
  7.         System.out.println(returnTest());  
  8.     }  
  9.   
  10.     public static boolean returnTest()  
  11.     {  
  12.         try  
  13.         {  
  14.             System.out.println("try块");  
  15.             return true;  
  16.         }   
  17.         finally  
  18.         {  
  19.             System.out.println("finally块");  
  20.             return false;  
  21.         }  
  22.     }  
  23. }  
执行结果:

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. try块  
  2. finally块  
  3. false  
注:finally里的return覆盖掉了try里的return;

(3)如果try后面有个catch块,里面有return语句,那么finally语句会不会执行?

代码:

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. //Java try-catch-finally-return执行问题  
  2. public class Test1  
  3. {  
  4.     public static void main(String[] args)  
  5.     {  
  6.         System.out.print(returnTest());  
  7.     }  
  8.   
  9.     public static boolean returnTest()  
  10.     {  
  11.         int temp = 23;  
  12.         try  
  13.         {  
  14.             System.out.println("try块");  
  15.             temp = temp/0;  
  16.             return true;  
  17.         }   
  18.         catch (Exception e)  
  19.         {  
  20.             System.out.println("catch块");  
  21.             return false;  
  22.         }   
  23.         finally  
  24.         {  
  25.             System.out.println("finally块");  
  26.         }  
  27.     }  
  28. }  
执行结果:

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. try块  
  2. catch块  
  3. finally块  
  4. false  
在finally块里也加上return语句:

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. //Java try-catch-finally-return执行问题  
  2. public class Test1  
  3. {  
  4.     public static void main(String[] args)  
  5.     {  
  6.         System.out.print(returnTest());  
  7.     }  
  8.   
  9.     public static boolean returnTest()  
  10.     {  
  11.         int temp = 23;  
  12.         try  
  13.         {  
  14.             System.out.println("try块");  
  15.             temp = temp/0;  
  16.             return true;  
  17.         }   
  18.         catch (Exception e)  
  19.         {  
  20.             System.out.println("catch块");  
  21.             return true;  
  22.         }   
  23.         finally  
  24.         {  
  25.             System.out.println("finally块");  
  26.             return false;  
  27.         }  
  28.     }  
  29. }  
结果:

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. try块  
  2. catch块  
  3. finally块  
  4. false  
注:如果finally里没有return语句,而catch里有return语句,则执行catch里的return语句;     而如果finally里也有return语句,则返回finally里的return语句;即catch块不会影响finally的执行; finally语句块的作用就是为了保证无论出现什么情况,一定要执行的,那么finally里的代码肯定会执行,并且是在return前执行;

又如:执行结果是多少呢;

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. //Java try-catch-finally-return执行问题  
  2. public class Test1  
  3. {  
  4.     public static void main(String[] args)  
  5.     {  
  6.         System.out.print(returnTest());  
  7.     }  
  8.   
  9.     public static int returnTest()  
  10.     {  
  11.         int temp = 23;  
  12.         try  
  13.         {  
  14.             System.out.println("try块");  
  15.             return temp += 88;  
  16.         }   
  17.         catch (Exception e)  
  18.         {  
  19.             System.out.println("catch块");  
  20.         }   
  21.         finally  
  22.         {  
  23.             if (temp > 25)  
  24.             {  
  25.                 System.out.println("b>25:" + temp);  
  26.             }  
  27.             System.out.println("finally块");  
  28.         }  
  29.         return temp;  
  30.     }  
  31. }  
执行结果:

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. try块  
  2. b>25:111  
  3. finally块  
  4. 111  
注:并不是try语句中return执行完之后才执行的finally;     而是在执行return b+=88时,分成了两步,先b+=88;再return b;      将return temp;放到System.out.println("finally块");后面,输出结果不变;

如果将finally语句改为:

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. finally  
  2.     {  
  3.         if (temp > 25)  
  4.         {  
  5.             System.out.println("b>25:" + temp);  
  6.         }  
  7.         System.out.println("finally块");  
  8.         temp = 100;  
  9.     }  
如果finally没有返回语句覆盖的话,那原来的返回值就不会变,不管你是不是改变了要返回的哪个变量,所以返回的值依然不变;
关注
打赏
1661269038
查看更多评论
立即登录/注册

微信扫码登录

0.0501s