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 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; } } |
运行代码:
|
1
2
3
4
5
|
public static void main(String[] args) { System.out.println("=============NoException=================="); System.out.println(NoException()); System.out.println("==============================="); } |
运行结果:
|
1
2
3
4
5
|
=============NoException==================i in try block is:10i in finally - from try or catch block is:98=============================== |
执行顺序:
执行try块,执行到return语句时,先执行return的语句,--i,但是不返回到main方法,执行finally块,遇到finally块中的return语句,执行--i,并将值返回到main方法,这里就不会再回去返回try块中计算得到的值。
结论:try-catch-finally都有return语句时,没有异常时,返回值是finally中的return返回的。
2.try块中没有抛出异常,仅try和catch中有return语句
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
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; }} |
运行结果:
|
1
2
3
4
5
6
|
=============NoException1==================i in try block is:10i in finally - from try or catch block is:9i in finally block is:89=============================== |
执行顺序:
try中执行完return的语句后,不返回,执行finally块,finally块执行结束后,返回到try块中,返回i在try块中最后的值。
结论:try-catch都有return语句时,没有异常时,返回值是try中的return返回的。
3.try块中抛出异常,try、catch和finally中都有return语句
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
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; }} |
执行结果:
|
1
2
3
4
5
6
7
8
|
=============WithException==================i in try block is:10i in catch - form try block is:10i in catch block is:9i in finally - from try or catch block is--8i in finally block is--76=============================== |
执行顺序:
抛出异常后,执行catch块,在catch块的return的--i执行完后,并不直接返回而是执行finally,因finally中有return语句,所以,执行,返回结果6。
结论:
try块中抛出异常,try、catch和finally中都有return语句,返回值是finally中的return。
4.try块中抛出异常,try和catch中都有return语句
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
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; }} |
执行结果:
|
1
2
3
4
5
6
7
|
=============WithException1==================i in try block is:10i in catch - form try block is:10i in finally - from try or catch block is:9i in finally block is:89=============================== |
执行顺序:
抛出异常后,执行catch块,执行完finally语句后,依旧返回catch中的执行return语句后的值,而不是finally中修改的值。
结论:
返回的catch中return值。
5.try、catch中都出现异常,在finally中有返回
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
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;} |
执行结果:
|
1
2
3
4
5
6
7
|
=============WithException2==================i in try block is:10i in catch - form try block is:10i in finally - from try or catch block is:10i in finally block is:87=============================== |
执行顺序:
try块中出现异常到catch,catch中出现异常到finally,finally中执行到return语句返回,不检查异常。
结论:
返回finally中return值。
6、只在函数最后出现return语句
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
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;} |
执行结果:
|
1
2
3
4
5
6
7
|
=============WithException3==================i in try block is:10i in catch - form try block is:10i in finally - from try or catch block is:10i in finally block is:87=============================== |
总体结论:
结论一:
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返回值执行的顺序(区别)的更多相关文章
- try--catch--finally中return返回值执行的顺序
1.try块中没有抛出异常,try.catch和finally块中都有return语句 public static int NoException(){ int i=10; try{ System.o ...
- 用jquery的ajax方法获取return返回值的正确姿势
如果jquery中,想要获取ajax的return返回值,必须注意两方面,ajax的同步异步问题,在ajax方法里面还是外面进行return返回值. 下面列举了三种写法,如果想成功获取到返回值,参考第 ...
- 用jquery的ajax方法获取不到return返回值
如果jquery中,获取不到ajax返回值. 两个错误写法会导致这种情况:1.ajax未用同步 2.在ajax方法中直接return返回值. 下面列举了三种写法,如果想成功获取到返回值,参考第三种写法 ...
- 关于ExecuteNonQuery执行存储过程的返回值 、、实例讲解存储过程的返回值与传出参数、、、C#获取存储过程的 Return返回值和Output输出参数值
关于ExecuteNonQuery执行存储过程的返回值 用到过ExecuteNonQuery()函数的朋友们在开发的时候肯定这么用过. if(cmd.ExecuteNonQuery("xxx ...
- c++中带返回值函数没写return能通过编译但运行时会出现奇怪问题
c++中带返回值函数没写return能通过编译但运行时会出现奇怪问题 例如: string myFunc(){ theLogics(); } 发现调用: myFunc(); 崩溃. 但调用: cout ...
- C# 调用存储过程操作 OUTPUT参数和Return返回值
本文转载:http://www.cnblogs.com/libingql/archive/2010/05/02/1726104.html 存储过程是存放在数据库服务器上的预先编译好的sql语句.使用存 ...
- [改善Java代码]不要在finally块中处理返回值
在finally代码块中处理返回值,这是在面试题中经常出现的题目.但是在项目中绝对不能再finally代码块中出现return语句,这是因为这种处理方式非常容易产生"误解",会严重 ...
- 字节码分析finally块对return返回值的影响
直接进入主题.看如下代码: public int test(){ int i=0; try { i=1; return i; } catch (Exception e) { i=2; return i ...
- Asp.net MVC 中Controller返回值类型ActionResult
[Asp.net MVC中Controller返回值类型] 在mvc中所有的controller类都必须使用"Controller"后缀来命名并且对Action也有一定的要求: 必 ...
随机推荐
- 安装LockBox 3.7.0,用LockBox加密解密!
LockBox 官 方 网 站:http://lockbox.seanbdurkin.id.au/HomePage LockBox的Github 网址:https://github.com/S ...
- css相对定位+浮动实现元素位置互换
1.设置元素透明度 opacity:0.5; // w3c filter:alpha(opacity=50); //IE 2 position:relative; float:left; 一起使用的效 ...
- Visual Studio 2013 添加新项缺失[ADO.NET 实体数据模型]解决方法
解决方法如下: 1).到 Visual Studio 2013 的安装包的目录:/packages/EFTools 中寻找如下文件 EFTools.msi; EFTools.cab,如图 将它们复制自 ...
- 整整十年 - Agent Framework for TypeScript 2.0
十年前,我发布了 Agent Framework for .NET 2.0 今天,Agent 又开始了新的旅程, 这次支持的语言是 TypeScript 2.0 上需求:init函数只能被调用一次 废 ...
- RDD常用方法之subtract&intersection&cartesian
subtract Return an RDD with the elements from `this` that are not in `other` . def subtract(othe ...
- oracle ebs应用产品安全性-交叉验证规则
转自: http://blog.itpub.net/298600/viewspace-625138/ 定义: Oracle键弹性域可以根据自定义键弹性域时所定义的规则,执行段值组合的自动交叉验证.使用 ...
- 针对“Can't download driver to specified address”错误
当用h-flasher检测flash-id时,可能会出现上述错误,个人认为当提示这个错误时,一般来说是sdram出现了问题,可以看看sdram有没有虚焊,或者周围的电阻电容是否正确. 在后来调板子的过 ...
- setContentType、setCharacterEncoding、pageEncoding和contentType
request.setCharacterEncoding()是设置从request中取得的值或从数据库中取出的值 response.setContentType("text/html;cha ...
- [转]解决a different object with the same identifier value was already associated with the session错误
1.a different object with the same identifier value was already associated with the session. 错误原因:在h ...
- Log4net 记录日志
配置文件 备注,需要放到根目录下面 <?xml version="1.0" encoding="utf-8"?> <configuration ...