• 正常执行流程
try执行,遇到异常就跳到catch执行(以使得程序不会崩溃);
不管有没有异常catch,最后都执行finally
 
  • 含return语句执行流程分析
若try块中return 可达, return语句(如:return x=x+1;)
  1. 对x执行运算x=x+1 (若有运算)
  2. 复制一个变量x给try的return语句(按值复制:基本类型就是值本身,对象就是地址值)
  3. 但return语句并不马上返回,控制权转移到finally块中执行: (1)若finally中无return语句:finally执行完后,try块执行return语句返回之前复制的变量x(基本类型就是值本身,对象就是地址值)(所以:若x为基本类型,fianlly中改变x对最终return结果无效;若x为对象类型,按地址值传递可以改变x的最终return结果)    (2)若finally中有return语句:执行后直接返回(即“覆盖try中return语句”:try中return语句不再返回)
 
若try中return 不可达(exception 在return前出现
    exception出现后控制权直接转到catch 块(即try中 exception之后的语句<包括return>不会执行),最后到finally块(catch到finally中流程与上面try到finally中流程相同)
 
 
  • finally不执行的特殊情况
  1. if you call System.exit() or
  2. if the JVM crashes first
 
  • A return statement in the finally block is a bad idea
By doing a return from the finally block, you suppress the exception entirely.(finally 中有return ,try,catch的 return throw都不会再被调用)
  1. publicstaticint getANumber(){
  2.     try{
  3.         thrownewNoSuchFieldException();
  4.     } finally {
  5.         return43;
  6.     }
  7. }
Running the method above will return a “43” and the exception in the try block will not be thrown. This is why it is considered to be a very bad idea to have a return statement inside the finally block.
 
 

例子解释:
 
执行流程
  1. If the return in the try block is reached, it transfers control to the finally block, and the function eventually returns normally (not a throw).
  2. If an exception occurs, but then the code reaches a return from the catch block, control is transferred to the finally block and the function eventually returns normally (not a throw).
  3. In your example, you have a return in the finally, and so regardless of what happens, the function will return 34, because finally has the final (if you will) word.
 
 
finally语句在try或catch中的return语句执行之后、return返回之前执行的

  • 在try中含有return+基本类型情况:
  1. publicclassFinallyTest1{
  2.     publicstaticvoid main(String[] args){
  3.         System.out.println(test1());
  4.     }
  5.     publicstaticint test1(){
  6.         int b =20;
  7.         try{
  8.             System.out.println("try block");
  9.             return b +=80;
  10.         }
  11.         catch(Exception e){
  12.             System.out.println("catch block");
  13.         }
  14.         finally {
  15.             System.out.println("finally block");
  16.             if(b >25){
  17.                 System.out.println("b>25, b = "+ b);
  18.             }
  19.         }
  20.         return b;
  21.     }
  22. }
运行结果:
  1. try block
  2. finally block
  3. b>25, b =100
  4. 100
 
  • 在catch中含有return+基本类型情况(分析跟在try中含有return情况一样):
  1. publicclassTest{
  2.  
  3.     publicstaticvoid main(String[] args){
  4.  
  5.         System.out.println(test1());
  6.     }
  7.  
  8.     publicstaticint test1(){
  9.         int b =20;
  10.  
  11.         try{
  12.             int a =1/0;//触发异常
  13.             System.out.println("try block");//不会被执行
  14.             return b +=80;//不会被执行
  15.         }
  16.         catch(Exception e){
  17.             System.out.println("catch block");
  18.             return b +=180;//最后在此返回
  19.         }
  20.         finally {
  21.             System.out.println("finally block");
  22.             if(b >25){
  23.                 System.out.println("b>25, b = "+ b);
  24.             }
  25.         }
  26. //        return b;
  27.     }
  28.  
  29. }
执行结果:
  1. catch block
  2. finally block
  3. b>25, b =200
  4. 200
 
  • 在try中含有return+对象类型情况:
  1. publicclassTest{
  2.  
  3.     publicstaticvoid main(String[] args){
  4.         System.out.println(getMap().get("KEY").toString());
  5.     }
  6.  
  7.     publicstaticMap<String,String> getMap(){
  8.         Map<String,String>map=newHashMap<String,String>();
  9.         map.put("KEY","INIT");
  10.  
  11.         try{
  12.             map.put("KEY","TRY");
  13.             returnmap;//return在控制权转移到finally前:复制了一个map对象的地址值(对象按地址值传递)
  14.         }
  15.         catch(Exception e){
  16.             map.put("KEY","CATCH");
  17.         }
  18.         finally {
  19.             map.put("KEY","FINALLY");//根据对象地址值修改对象内容(按地址值传递),因此会影响try中return返回的对象内容
  20.             map= null;//map为null即不再指向该对象,
  21.             // 但由于前面return复制了一个对象的引用(地址值),而对象是被分配在堆中的,只要有引用指向这个对象,系统就不会回收此对象, 所以此处map = null 并不会影响最后try中return返回对象内容
  22.         }
  23.  
  24.         returnmap;
  25.     }
  26.  
  27. }
执行结果:
  1. FINALLY

  • 在catch中含有return+基本类型情况(分析跟在try中含有return情况一样):
  1. publicclassTest{
  2.  
  3.     publicstaticvoid main(String[] args){
  4.         System.out.println(getMap().get("KEY").toString());
  5.     }
  6.  
  7.     publicstaticMap<String,String> getMap(){
  8.         Map<String,String>map=newHashMap<String,String>();
  9.         map.put("KEY","INIT");
  10.  
  11.         try{
  12.             int a =1/0;//触发异常
  13.             map.put("KEY","TRY");//不会被执行
  14.             returnmap;//不会被执行
  15.         }
  16.         catch(Exception e){
  17.             map.put("KEY","CATCH");
  18.             returnmap;//return在控制权转移到finally前:复制了一个map对象的地址值(对象按地址值传递)
  19.         }
  20.         finally {
  21.             map.put("KEY","FINALLY");//根据对象地址值修改对象内容(按地址值传递),因此会影响catch中return返回的对象内容
  22.             map= null;//map为null即不再指向该对象,
  23.             // 但由于前面return复制了一个对象的引用(地址值),而对象是被分配在堆中的,只要有引用指向这个对象,系统就不会回收此对象,
  24.             // 所以此处map = null 并不会影响最后catch中return返回对象内容
  25.         }
  26.  
  27. //        return map;
  28.     }
  29.  
  30. }
执行结果:
  1. FINALLY
 
 
finally块中的return语句会覆盖try块中的return返回
  1. publicclassTest{
  2.  
  3.     publicstaticvoid main(String[] args){
  4.  
  5.         System.out.println(test2());
  6.     }
  7.  
  8.     publicstaticint test2(){
  9.         int b =20;
  10.  
  11.         try{
  12.             System.out.println("try block");
  13.             return b +=80;
  14.         }catch(Exception e){
  15.             System.out.println("catch block");
  16.         } finally {
  17.             System.out.println("finally block");
  18.             if(b >25){
  19.                 System.out.println("b>25, b = "+ b);
  20.             }
  21.             return200;
  22.         }
  23.         // return b;
  24.     }
  25.  
  26. }
执行结果:
  1. try block
  2. finally block
  3. b>25, b =100
  4. 200
 
参考:http://www.cnblogs.com/lanxuezaipiao/p/3440471.html(博文代码用例4结果有误,见该博文讨论区分析)
 
 
 
 
 
 
 

JAVA try-catch-finally-return的更多相关文章

  1. java try catch finally return执行

    public static int testBasic(){ int i = 1; try{ i++; System.out.println("try block, i = "+i ...

  2. 【Java疑难杂症】有return的情况下try catch finally的执行顺序

    有这样一个问题,异常处理大家应该都不陌生,类似如下代码: public class Test { public static void main(String[] args) { int d1 = 0 ...

  3. 可惜Java中没有yield return

    项目中一个消息推送需求,推送的用户数几百万,用户清单很简单就是一个txt文件,是由hadoop计算出来的.格式大概如下: uid caller 123456 12345678901 789101 12 ...

  4. 异常 try catch finally return 执行关系 MD

    Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...

  5. Java_try,catch,finally return之间的执行顺序

    以往认为函数只要执行到return语句便会返回结果并终止,然而这时错误的,因为这存在特例. 掌握下面几条原则就可以完全解决“当try.catch.finally遭遇return”的问题. 原则:1.f ...

  6. try catch finally return之间的关系

    一.try catch finally return之间的关系: 正在写dsoFramer的时候,同事突然说面试的时候问的一个问题,catch和return那个先执行,我瞬间迷茫了,然后整理了整理,稍 ...

  7. Intellij Idea 12 开发Android 报Caused by: java.lang.UnsatisfiedLinkError: FindLibrary return null;

    这次开发是用的百度地图api,导入两个so文件,结果启动的时候总是报Caused by: java.lang.UnsatisfiedLinkError: findlibrary return null ...

  8. Hint: Fallback method 'public java.lang.String queryUserByIdFallback(java.lang.Long)' must return: User or its subclass

    1.错误日志 熔断器添加错误方法返回时,报了一个 error. com.netflix.hystrix.contrib.javanica.exception.FallbackDefinitionExc ...

  9. try catch finally return运行顺序

    首先让我们搞懂两组概念:try catch finally和return 1.try catch finally 首先说try catch, (1)try语句 ,try语句用来包围可能出现异常的代码片 ...

  10. 理清Java中try-catch-finally带return的执行顺序

    前言:try-catch-finally带return和异常时,它们之间执行顺序问题是留下来的一个小疑问,今天搞清楚它们 第一种情况:无异常 //1.try-catch-finally都带有retur ...

随机推荐

  1. 《CSS那些事儿》读书笔记

    注: 此书出版于2009年,所以有些知识...你懂得. 有些我熟悉的知识点,就没有记录下来了,所以想了解更多的细节,还是去看下此书吧. 暗灰色标记部分,是我自己的理解,有不对或要补充的地方,还请大家多 ...

  2. idea 多模块项目依赖父工程class找不到问题

    比如,我们有这么个过程,项目结构如下: a --b --c a是总结点,b是子节点,c是父节点 b依赖父节点class,通过maven构建时通常我们会在子节点中添加父节点依赖,如: <depen ...

  3. [转]C++基本功和 Design Pattern系列 ctor & dtor

    今天Aear讲的是class.ctor 也就是constructor, 和  class.dtor, destructor. 相信大家都知道constructor 和 destructor是做什么用的 ...

  4. Wireshark抓包、过滤器

    查阅于http://blog.sina.com.cn/s/blog_5d527ff00100dwph.html 1.捕捉过滤器 设置捕捉过滤器的步骤是:- 选择 capture -> optio ...

  5. css3之box-sizing

    css盒子模型中包括几种重要的属性,包括margin.border.padding以及content.但浏览器对其盒子模型的解释则有所不痛,启用标准模式还是IE(怪)模式是与当前页面的文档声明相关的. ...

  6. 纯js制作遮罩层对话框 -- g皓皓

    //本文支持js在线工具测试.转载请注明出处. <htmlxmlns="http://www.w3.org/1999/xhtml"> <head> < ...

  7. Windows 7如何限制运行特定的应用程序(转载)

    AppLocker即"应用程序控制策略",是Windows 7系统中新增加的一项安全功能. 步骤/方法 1 单击"开始"菜单,单击"控制面板" ...

  8. php 实用函数

    第一次随笔,写一些自己工作当中比较实用的函数吧. 数组函数: 1 array_column --返回数组当中指定的一列 用法一:返回数组当中指定的一列 应用场景:取出全班同学的id,去其他表查询这些同 ...

  9. history对象属性和方法

    history对象保存着用户上网的历史记录,从窗口被打开的那一刻算起,history是window对象的属性,因此每个浏览器窗口.每个标签页乃至每个框架,都有自 己的history对象和特定的wind ...

  10. python:open/文件操作

    open/文件操作f=open('/tmp/hello','w') #open(路径+文件名,读写模式) #读写模式:r只读,r+读写,w新建(会覆盖原有文件),a追加,b二进制文件.常用模式 如:' ...