JAVA try-catch-finally-return
- 正常执行流程:
- 含return语句执行流程分析:

- 对x执行运算x=x+1 (若有运算)
- 复制一个变量x给try的return语句(按值复制:基本类型就是值本身,对象就是地址值)
- 但return语句并不马上返回,控制权转移到finally块中执行: (1)若finally中无return语句:finally执行完后,try块执行return语句返回之前复制的变量x(基本类型就是值本身,对象就是地址值)(所以:若x为基本类型,fianlly中改变x对最终return结果无效;若x为对象类型,按地址值传递可以改变x的最终return结果) (2)若finally中有return语句:执行后直接返回(即“覆盖try中return语句”:try中return语句不再返回)
- finally不执行的特殊情况:
- if you call System.exit() or
- if the JVM crashes first

- A return statement in the finally block is a bad idea:
publicstaticint getANumber(){try{thrownewNoSuchFieldException();} finally {return43;}}

- 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).
- 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).
- 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.
- 在try中含有return+基本类型情况:
publicclassFinallyTest1{publicstaticvoid main(String[] args){System.out.println(test1());}publicstaticint test1(){int b =20;try{System.out.println("try block");return b +=80;}catch(Exception e){System.out.println("catch block");}finally {System.out.println("finally block");if(b >25){System.out.println("b>25, b = "+ b);}}return b;}}
try blockfinally blockb>25, b =100100
- 在catch中含有return+基本类型情况(分析跟在try中含有return情况一样):
publicclassTest{publicstaticvoid main(String[] args){System.out.println(test1());}publicstaticint test1(){int b =20;try{int a =1/0;//触发异常System.out.println("try block");//不会被执行return b +=80;//不会被执行}catch(Exception e){System.out.println("catch block");return b +=180;//最后在此返回}finally {System.out.println("finally block");if(b >25){System.out.println("b>25, b = "+ b);}}// return b;}}
catch blockfinally blockb>25, b =200200
- 在try中含有return+对象类型情况:
publicclassTest{publicstaticvoid main(String[] args){System.out.println(getMap().get("KEY").toString());}publicstaticMap<String,String> getMap(){Map<String,String>map=newHashMap<String,String>();map.put("KEY","INIT");try{map.put("KEY","TRY");returnmap;//return在控制权转移到finally前:复制了一个map对象的地址值(对象按地址值传递)}catch(Exception e){map.put("KEY","CATCH");}finally {map.put("KEY","FINALLY");//根据对象地址值修改对象内容(按地址值传递),因此会影响try中return返回的对象内容map= null;//map为null即不再指向该对象,// 但由于前面return复制了一个对象的引用(地址值),而对象是被分配在堆中的,只要有引用指向这个对象,系统就不会回收此对象,所以此处map = null 并不会影响最后try中return返回对象内容}returnmap;}}
FINALLY
- 在catch中含有return+基本类型情况(分析跟在try中含有return情况一样):
publicclassTest{publicstaticvoid main(String[] args){System.out.println(getMap().get("KEY").toString());}publicstaticMap<String,String> getMap(){Map<String,String>map=newHashMap<String,String>();map.put("KEY","INIT");try{int a =1/0;//触发异常map.put("KEY","TRY");//不会被执行returnmap;//不会被执行}catch(Exception e){map.put("KEY","CATCH");returnmap;//return在控制权转移到finally前:复制了一个map对象的地址值(对象按地址值传递)}finally {map.put("KEY","FINALLY");//根据对象地址值修改对象内容(按地址值传递),因此会影响catch中return返回的对象内容map= null;//map为null即不再指向该对象,// 但由于前面return复制了一个对象的引用(地址值),而对象是被分配在堆中的,只要有引用指向这个对象,系统就不会回收此对象,// 所以此处map = null 并不会影响最后catch中return返回对象内容}// return map;}}
FINALLY
publicclassTest{publicstaticvoid main(String[] args){System.out.println(test2());}publicstaticint test2(){int b =20;try{System.out.println("try block");return b +=80;}catch(Exception e){System.out.println("catch block");} finally {System.out.println("finally block");if(b >25){System.out.println("b>25, b = "+ b);}return200;}// return b;}}
try blockfinally blockb>25, b =100200
JAVA try-catch-finally-return的更多相关文章
- java try catch finally return执行
public static int testBasic(){ int i = 1; try{ i++; System.out.println("try block, i = "+i ...
- 【Java疑难杂症】有return的情况下try catch finally的执行顺序
有这样一个问题,异常处理大家应该都不陌生,类似如下代码: public class Test { public static void main(String[] args) { int d1 = 0 ...
- 可惜Java中没有yield return
项目中一个消息推送需求,推送的用户数几百万,用户清单很简单就是一个txt文件,是由hadoop计算出来的.格式大概如下: uid caller 123456 12345678901 789101 12 ...
- 异常 try catch finally return 执行关系 MD
Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...
- Java_try,catch,finally return之间的执行顺序
以往认为函数只要执行到return语句便会返回结果并终止,然而这时错误的,因为这存在特例. 掌握下面几条原则就可以完全解决“当try.catch.finally遭遇return”的问题. 原则:1.f ...
- try catch finally return之间的关系
一.try catch finally return之间的关系: 正在写dsoFramer的时候,同事突然说面试的时候问的一个问题,catch和return那个先执行,我瞬间迷茫了,然后整理了整理,稍 ...
- Intellij Idea 12 开发Android 报Caused by: java.lang.UnsatisfiedLinkError: FindLibrary return null;
这次开发是用的百度地图api,导入两个so文件,结果启动的时候总是报Caused by: java.lang.UnsatisfiedLinkError: findlibrary return null ...
- 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 ...
- try catch finally return运行顺序
首先让我们搞懂两组概念:try catch finally和return 1.try catch finally 首先说try catch, (1)try语句 ,try语句用来包围可能出现异常的代码片 ...
- 理清Java中try-catch-finally带return的执行顺序
前言:try-catch-finally带return和异常时,它们之间执行顺序问题是留下来的一个小疑问,今天搞清楚它们 第一种情况:无异常 //1.try-catch-finally都带有retur ...
随机推荐
- 跟我学android-Notification
Notification 可以理解为通知的意思,会出现在通知栏,比如来了一条短信 使用 Notification 有以下3个步骤: 1. 创建 NotificationManager的对象 2.为No ...
- 02_ Windows与Linux双重引导
1. Grub2引导window. ---------------------步骤1--------------------------------- vim /etc/grub.d/40_custo ...
- underscorejs-map学习
2.2 map 2.2.1 语法: _.map(list, iteratee, [context]) 2.2.2 说明: 对集合的每个成员依次进行某种操作,将返回的值依次存入一个新的数组.接收3个参数 ...
- [Git]Git远程仓库
1.创建ssh key 查看主目录下面 C:\Users\Administrator\.ssh 是否存在 id_rsa 和 id_rsa.pub 文件,如果不存在需要generate new key. ...
- php面向对象编程--学习笔记
1.声明一个类 在php中使用class关键字创建一个新类,类包括属性与方法.语法格式如下: <?php class类名{ 属性: 方法: } ?> 2.创建一个实例对象 创建对象的过程称 ...
- 未能写入输出文件 c:/WINDOWS/Microsoft.NET/Framework/v2.0.50727/Temporary
ERROR: 未能写入输出文件“c:/WINDOWS/Microsoft.NET/Framework/v2.0.50727/Temporary ASP.NET Files/root/.... 一般遇到 ...
- The first time
早上起来发现昨晚申请的博客开通了,\(^o^)/~纪念下,就像刚打代码学的hello world一样,用c,c++写一个程序纪念下.O(∩_∩)O~哈哈哈哈哈. /*C hello world程序*/ ...
- poj Fishnet
http://poj.org/problem?id=1408 #include<cstdio> #include<cstring> #include<cmath> ...
- 分析智能卡的ATR格式
一些例子 NXP 080=========3b f8 T0 Y1 = 0xF(TA1, TB1, TC1, TD1), K = 813 TA1 F = 0x1(Fi = 372, Fmax = 5 ...
- Vista UAC下应用程序标注为“需要管理员权限”的四种方法
在Microsoft 的UACBlog里对此有过叙述.总结如下: 首先,如果一个程序被识别为管理员程序时,Vista会在它的图标上加上一个盾牌标记.因此很容易看出来. 其次,如果UAC有效的话,管理员 ...