关于try-catch-finally中的return

分为以下几种情况:

举例:

情况1try{} catch(){}finally{} return;

            显然程序按顺序执行。

情况2try{ return; }catch(){} finally{} return;

         程序执行try块中return之前(包括return语句中的表达式运算)代码;

         再执行finally块,最后执行tryreturn;

         finally块之后的语句return,因为程序在try中已经return所以不再执行。

public static int TryCF(){

        int x = 1;

try{

x = x + 1;

System.out.println("try: x = " + x);

return x+1;

}

catch(Exception e){

System.out.println("catch: x = " + x);

return x+2;

}

finally{

x = x + 3;

System.out.println("finally: x = " + x);

}

// return x+3;   //出错需要注释掉

}

输出:

try: x = 2

finally: x = 5

x的最终值:3      注意:return当前块中的数据3,而不是最终的x5

 

情况3try{ } catch(){return;} finally{} return;

         程序先执行try,如果遇到异常执行catch块,

         有异常:则执行catchreturn之前(包括return语句中的表达式运算)代码,再执行finally语句中全部代码,最后执行catch块中return. finally之后代码不再执行。

         无异常:执行完tryfinallyreturn.

public static int TryCF(){

        int x = 1;

try{

x = x + 1;

x = 2/0;

System.out.println("try: x = " + x);

}

catch(Exception e){

System.out.println("catch: x = " + x);

return x+2;

}

finally{

x = x + 3;

System.out.println("finally: x = " + x);

}

return x+3;

}

输出:

catch: x = 2

finally: x = 5

x的最终值:4

情况4try{ return; }catch(){} finally{return;}

          程序执行try块中return之前(包括return语句中的表达式运算)代码;

          再执行finally块,因为finally块中有return所以提前退出。

code

public static int TryCF(){

        int x = 1;

try{

x = x + 1;

System.out.println("try: x = " + x);

return x+1;

}

catch(Exception e){

System.out.println("catch: x = " + x);

return x+2;

}

finally{

x = x + 3;

System.out.println("finally: x = " + x);

return x+3;

}

输出:

try: x = 2

finally: x = 5

x的最终值:8

情况5try{} catch(){return;}finally{return;}

          程序执行catch块中return之前(包括return语句中的表达式运算)代码;

          再执行finally块,因为finally块中有return所以提前退出。

public static int TryCF(){

        int x = 1;

try{

x = x + 1;

x = 2/0;

System.out.println("try: x = " + x);

}

catch(Exception e){

System.out.println("catch: x = " + x);

return x+2;

}

finally{

x = x + 3;

System.out.println("finally: x = " + x);

return x+3;

}

}

输出:

catch: x = 2

finally: x = 5

x的最终值:8

情况6try{ return;}catch(){return;} finally{return;}

          程序执行try块中return之前(包括return语句中的表达式运算)代码;

          有异常:执行catch块中return之前(包括return语句中的表达式运算)代码;

                       则再执行finally块,因为finally块中有return所以提前退出。

          无异常:则再执行finally块,因为finally块中有return所以提前退出。

 

有异常:

public static int TryCF(){

        int x = 1;

try{

x = x + 1;

x = 2/0;

System.out.println("try: x = " + x);

return x+1;

}

catch(Exception e){

System.out.println("catch: x = " + x);

return x+2;

}

finally{

x = x + 3;

System.out.println("finally: x = " + x);

return x+3;

}

}

输出:

catch: x = 2

finally: x = 5

x的最终值:8

无异常:

public static int TryCF(){

        int x = 1;

try{

x = x + 1;

System.out.println("try: x = " + x);

return x+1;

}

catch(Exception e){

System.out.println("catch: x = " + x);

return x+2;

}

finally{

x = x + 3;

System.out.println("finally: x = " + x);

return x+3;

}

}

输出:

try: x = 2

finally: x = 5

x的最终值:8

 

对于情况3绿了有如下解释:

因为java都是按值传递的。return的若是对象,则先把对象的副本保存起来,也就是说保存的是指向对象的地址。若对原来的对象进行修改。对象的地址仍然不变,return的副本仍然是指向这个对象,所用finally中对对象的修改仍然有作用。而基本数据类型保存的是原原本本的数据,return保存副本后,在finally中修改都是修改原来的数据。副本中的数据还是不变,所以finally中修改对return无影响。

Code:

private static List<string> test1()  

       {  

           List<string> strlist = new List<string>();  

           strlist.Add("zs");  

           strlist.Add("ls");  

           strlist.Add("ww");  

           strlist.Add("mz");  

           try  

           {  

               strlist.Add("wq");  

               return strlist;  

           }  

           finally  

           {  

               strlist.Add("yyy");  

           }  

       }  

输出:

zs
ls
ww
mz
wq
yyy

 

关于返回值是否改变总结:

1如果finally中没有return语句,但是改变了要返回的值,这里有点类似与引用传递和值传递的区别,分以下两种情况:
1)如果return的数据是基本数据类型或文本字符串,则在finally中对该基本数据的改变不起作用,try中的return语句依然会返回进入finally块之前保留的值。
2)如果return的数据是引用数据类型,而在finally中对该引用数据类型的属性值的改变起作用,try中的return语句返回的就是在finally中改变后的该属性的值。

2、如果finally中有return 语句,就提前退出,返回的也是finally中的最终值(无论数据类型)

 

结论:

1、不管是否出现异常,finally块中代码都会执行;

2、当trycatch中有return时,finally仍然会执行;

3finally是在return后面的表达式运算后执行的(此时并没有返回运算后的值,而是先把要返回的值保存起来,管finally中的代码怎么样,返回的值都不会改变,任然是之前保存的值),所以函数返回值是在finally执行前确定的;

4finally中最好不要包含return,否则程序会提前退出,返回值不是trycatch中保存的返回值。

5、若finallyreturn,在try catch中有return,则执行完finally,直接返回去return ,不在执行fianlly以后的语句。

6任何执行try 或者catch中的return语句之前,都会先执行finally语句,如果finally存在的话。如果finally中有return语句,那么程序就return了,所以finally中的return是一定会被return的,编译器把finally中的return实现为一个warning提前退出。在哪个块中最终return 的值是本块的局部最终值(见绿)

 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值