1、try块中没有抛出异常,try、catch和finally块中都有return语句

 public static int NoException(){
int i=10;
try{
System.out.println("i in try block is:"+i);
return --i;
}
catch(Exception e){
--i;
System.out.println("i in catch - form try block is:"+i);
return --i;
}
finally{
System.out.println("i in finally - from try or catch block is:"+i);
return --i;
}
}
运行代码:

 public static void main(String[] args) {
System.out.println("=============NoException==================");
System.out.println(NoException());
System.out.println("===============================");
}
运行结果:

 =============NoException==================
i in try block is:10
i in finally - from try or catch block is:9
8
===============================
执行顺序:
   执行try块,执行到return语句时,先执行return的语句,--i,但是不返回到main方法,执行finally块,遇到finally块中的return语句,执行--i,并将值返回到main方法,这里就不会再回去返回try块中计算得到的值。
结论:try-catch-finally都有return语句时,没有异常时,返回值是finally中的return返回的。
 
2.try块中没有抛出异常,仅try和catch中有return语句
 public static int NoException1(){
int i=10;
try{
System.out.println("i in try block is:"+i);
return --i;
}
catch(Exception e){
--i;
System.out.println("i in catch - form try block is:"+i);
return --i;
}
finally{
System.out.println("i in finally - from try or catch block is:"+i);
--i;
System.out.println("i in finally block is:"+i);
//return --i;
}
}
运行结果:
 =============NoException1==================
i in try block is:10
i in finally - from try or catch block is:9
i in finally block is:8
9
===============================
执行顺序:
   try中执行完return的语句后,不返回,执行finally块,finally块执行结束后,返回到try块中,返回i在try块中最后的值。
结论:try-catch都有return语句时,没有异常时,返回值是try中的return返回的。
 
3.try块中抛出异常,try、catch和finally中都有return语句
 public static int WithException(){
int i=10;
try{
System.out.println("i in try block is:"+i);
i = i/0;
return --i;
}
catch(Exception e){
System.out.println("i in catch - form try block is:"+i);
--i;
System.out.println("i in catch block is:"+i);
return --i;
}
finally{
System.out.println("i in finally - from try or catch block is--"+i);
--i;
System.out.println("i in finally block is--"+i);
return --i;
}
}
执行结果:
 =============WithException==================
i in try block is:10
i in catch - form try block is:10
i in catch block is:9
i in finally - from try or catch block is--8
i in finally block is--7
6
===============================
执行顺序:
   抛出异常后,执行catch块,在catch块的return的--i执行完后,并不直接返回而是执行finally,因finally中有return语句,所以,执行,返回结果6。
结论:
   try块中抛出异常,try、catch和finally中都有return语句,返回值是finally中的return。
 
4.try块中抛出异常,try和catch中都有return语句
 public static int WithException1(){
int i=10;
try{
System.out.println("i in try block is:"+i);
i=i/0;
return --i;
}catch(Exception e){
System.out.println("i in catch - form try block is:"+i);
return --i;
}finally{ System.out.println("i in finally - from try or catch block is:"+i);
--i;
System.out.println("i in finally block is:"+i);
//return i;
}
}
执行结果:
 =============WithException1==================
i in try block is:10
i in catch - form try block is:10
i in finally - from try or catch block is:9
i in finally block is:8
9
===============================
执行顺序:
   抛出异常后,执行catch块,执行完finally语句后,依旧返回catch中的执行return语句后的值,而不是finally中修改的值。
结论:
   返回的catch中return值。
 
5.try、catch中都出现异常,在finally中有返回
 public static int WithException2(){
int i=10;
try{
System.out.println("i in try block is:"+i);
i=i/0;
return --i;
}
catch(Exception e){
System.out.println("i in catch - form try block is:"+i);
int j = i/0;
return --i;
}
finally{ System.out.println("i in finally - from try or catch block is:"+i);
--i;
--i;
System.out.println("i in finally block is:"+i);
return --i;
}
执行结果:
 =============WithException2==================
i in try block is:10
i in catch - form try block is:10
i in finally - from try or catch block is:10
i in finally block is:8
7
===============================
执行顺序:   
   try块中出现异常到catch,catch中出现异常到finally,finally中执行到return语句返回,不检查异常。
结论:
   返回finally中return值。
 
6、只在函数最后出现return语句
 public static int WithException3(){
int i=10;
try{
System.out.println("i in try block is:"+i);
i=i/0;
//return --i;
}
catch(Exception e){
System.out.println("i in catch - form try block is:"+i);
//int j = i/0;
//return --i;
}
finally{ System.out.println("i in finally - from try or catch block is:"+i);
--i;
--i;
System.out.println("i in finally block is:"+i);
//return --i;
}
return --i;
}
执行结果:

 =============WithException3==================
i in try block is:10
i in catch - form try block is:10
i in finally - from try or catch block is:10
i in finally block is:8
7
===============================
 
总体结论:
结论一:
   return语句并不是函数的最终出口,如果有finally语句,这在return之后还会执行finally(return的值会暂存在栈里面,等待finally执行后再返回)
结论二:
   finally里面不建议放return语句,根据需要,return语句可以放在try和catch里面和函数的最后。可行的做法有四:
   (1)return语句只在函数最后出现一次。
   (2)return语句仅在try和catch里面都出现。
   (3)return语句仅在try和函数的最后都出现。
   (4)return语句仅在catch和函数的最后都出现。
   注意,除此之外的其他做法都是不可行的,编译器会报错。
 
转载自:http://qing0991.blog.51cto.com/1640542/1387200

try--catch--finally中return返回值执行的顺序的更多相关文章

  1. try--catch--finally中return返回值执行的顺序(区别)

    1.try块中没有抛出异常,try.catch和finally块中都有return语句 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 public static int ...

  2. 用jquery的ajax方法获取return返回值的正确姿势

    如果jquery中,想要获取ajax的return返回值,必须注意两方面,ajax的同步异步问题,在ajax方法里面还是外面进行return返回值. 下面列举了三种写法,如果想成功获取到返回值,参考第 ...

  3. 用jquery的ajax方法获取不到return返回值

    如果jquery中,获取不到ajax返回值. 两个错误写法会导致这种情况:1.ajax未用同步 2.在ajax方法中直接return返回值. 下面列举了三种写法,如果想成功获取到返回值,参考第三种写法 ...

  4. 关于ExecuteNonQuery执行存储过程的返回值 、、实例讲解存储过程的返回值与传出参数、、、C#获取存储过程的 Return返回值和Output输出参数值

    关于ExecuteNonQuery执行存储过程的返回值 用到过ExecuteNonQuery()函数的朋友们在开发的时候肯定这么用过. if(cmd.ExecuteNonQuery("xxx ...

  5. c++中带返回值函数没写return能通过编译但运行时会出现奇怪问题

    c++中带返回值函数没写return能通过编译但运行时会出现奇怪问题 例如: string myFunc(){ theLogics(); } 发现调用: myFunc(); 崩溃. 但调用: cout ...

  6. C# 调用存储过程操作 OUTPUT参数和Return返回值

    本文转载:http://www.cnblogs.com/libingql/archive/2010/05/02/1726104.html 存储过程是存放在数据库服务器上的预先编译好的sql语句.使用存 ...

  7. [改善Java代码]不要在finally块中处理返回值

    在finally代码块中处理返回值,这是在面试题中经常出现的题目.但是在项目中绝对不能再finally代码块中出现return语句,这是因为这种处理方式非常容易产生"误解",会严重 ...

  8. 字节码分析finally块对return返回值的影响

    直接进入主题.看如下代码: public int test(){ int i=0; try { i=1; return i; } catch (Exception e) { i=2; return i ...

  9. Asp.net MVC 中Controller返回值类型ActionResult

    [Asp.net MVC中Controller返回值类型] 在mvc中所有的controller类都必须使用"Controller"后缀来命名并且对Action也有一定的要求: 必 ...

随机推荐

  1. python 类函数,实例函数,静态函数

    一.实现方法 class Function(object): # 在类定义中定义变量 cls_variable = "class varibale" def __init__(se ...

  2. 用Web Services来整合.NET和J2EE

    互用性(Interoperability)问题说起来容易但通常实现起来却比较困难.尽管Web service曾承诺要提供最佳的解决方案来衔接基于.NET和J2EE的应用程序,但其过程却并不简单.我们发 ...

  3. 【转】PHP 杂谈 坑爹的file_exists

    转自:http://www.cnblogs.com/baochuan/archive/2012/05/06/2445822.html 介绍   我发现了一个问题,今天与大家分享.我把整个过程描述一下. ...

  4. asp.net学习——Response对象

    (2011-03-29 07:33:03) 转载▼ 标签: 杂谈 分类: asp.net学习 响应的缓冲输出:为了提高服务器的性能,asp.net向浏览器Write的时候默认并不会每Write一次都会 ...

  5. C# 异步编程1 APM 异步程序开发

    C#已有10多年历史,单从微软2年一版的更新进度来看活力异常旺盛,C#中的异步编程也经历了多个版本的演化,从今天起着手写一个系列博文,记录一下C#中的异步编程的发展历程.广告一下:喜欢我文章的朋友,请 ...

  6. C++基础学习一(基础之基础)

    开篇:做了这么多年的软件,第一次使用博客的方式记录学习过程,之前都是笔记本(都有一摞了),因为之前一直从事的都是.NET的开发工作,对C++知之甚少,但一直想了解C++这门鼻祖级的语言,现在终于下定决 ...

  7. c# 百度地图api APP SN校验失败

    在使用c#调用百度地图Web服务api遇到的签名(sn校验)问题,在此记录一下,(ip白名单校验的请忽略) 1.首先获取ak与sk,这个两个东西可以从控制台中获取到 2.在这个地址:sn签名算法,里面 ...

  8. 【PAT】B1075 链表元素分类(25 分)

    这道题算有点难,心目中理想的难度. 不能前怕狼后怕虎,一会担心超时,一会又担心内存过大,直接撸 将三部分分别保存到vector 有意思的在于输出 分别输出第一个的add和num 中间输出nextadd ...

  9. ccf--20150303--节日

    本题思路:首先,计算a月1日是星期几,然后再通过b和c得出日期monday,最后判断monday是否合法. 题目与代码如下: 问题描述 试题编号: 201503-3 试题名称: 节日 时间限制: 1. ...

  10. ARC设置

    XCode兼容ARC和非ARC代码的方法 在ARC开发模式下引用非ARC文件或库需进行如下操作以告诉编译器此代码需按照非ARC模式对待: XCode中项目文件->TARGETS->Comp ...