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. React Native中Mobx的使用

    从今天开始我们来搞搞状态管理可否,这几天没怎么写博客,因为被病魔战胜了,tmd,突然的降温让我不知所措,大家最近注意安全,毕竟年底了,查的严,呸,大家注意保暖 特别声明:写该文只是写一下用MobX的思 ...

  2. 使用Twitter异常检测框架遇到的坑

    在Github上搜索“Anomaly Detection”,Twitter的异常检测框架(基于R语言)高居榜首,可见效果应该不错: 但是活跃度并不高,3-4年没人维护了: 因此在使用时难免会遇到一些坑 ...

  3. JMeter JMeter自身运行性能优化

    JMeter自身运行性能优化   by:授客 QQ:1033553122 测试环境 apache-jmeter-2.13   1.   问题描述 单台机器的下JMeter启动较大线程数时可能会出现运行 ...

  4. 安卓开发_关于WebView加载页面空白问题

    依据我自己的测试,发现有时候用APP打开网页的时候,有的网页加载成功之前需要很久,有的一下就出来了(比如百度) 当加载时间过长的情况下,这时候显示的是空白界面,其实不是代码问题,只是要打开的这个网页太 ...

  5. React JS和React-Native学习指南

    自己在学习React-Native过程中整理的一份学习指南,包含 教程.开源app和资源网站等,还在不断更新中.欢迎pull requests! React-Native学习指南本指南汇集React- ...

  6. il8n国际化

    il8n国际化 支持多国语言的web应用,根据客户端系统的语言类型返回对应的界面 方案 为每种语言提供一套相应的资源文件,并以规范化命名的方式保存在特定的目录中,由系统自动根据客户端语言选择适合的资源 ...

  7. [Android] 获取系统顶部状态栏(Status Bar)与底部导航栏(Navigation Bar)的高度

    Android一些设备都有上下两条bar,我们可以获取这些bar的信息.下面放上获取高度的代码.代码注释和其他方法有空再放. 原文地址请保留http://www.cnblogs.com/rossone ...

  8. springboot 学习之路 22 (读取自定义文件)

    springboot读取自定义的properties文件: package com.huhy.demo.properties; import lombok.Data; import org.sprin ...

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

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

  10. [20180718]拷贝数据文件从dg库.txt

    [20180718]拷贝数据文件从dg库.txt 1.测试环境:SCOTT@book> @ ver1PORT_STRING                    VERSION        B ...