public class Test {

     public static void main(String[] args) {
System.out.println("=============test1==================");
System.out.println(test1());
System.out.println("==============================="); System.out.println("=============test1_1==================");
System.out.println(test1_1());
System.out.println("==============================="); System.out.println("\n============test2===================");
System.out.println(test2());
System.out.println("==============================="); System.out.println("\n============test2_1===================");
System.out.println(test2_1());
System.out.println("==============================="); System.out.println("\n============test3===================");
System.out.println(test3());
System.out.println("==============================="); System.out.println("\n============test3_1===================");
System.out.println(test3_1());
System.out.println("===============================");
} public static String test1() {
String a = "in try"; try{
return a;
} catch ( Exception e ) { } finally {
a = "in finally";
System.out.println("do finally");
} return a;
} public static String test1_1() {
String a = "in try"; try{
return a;
} catch ( Exception e ) { } finally {
a = "in finally";
System.out.println("do finally");
return a;
}
} public static int test2() {
int a = 1; try{
return a;
} catch ( Exception e ) { } finally {
a = 2;
System.out.println("do finally");
} return a;
} public static int test2_1() {
int a = 1; try{
return a;
} catch ( Exception e ) { } finally {
a = 2;
System.out.println("do finally");
return a;
}
} public static Helper test3() {
Helper a = new Helper();
a.a = 1; try{
return a;
} catch ( Exception e ) { } finally {
a.a = 2;
System.out.println("do finally");
} return a;
} public static Helper test3_1() {
Helper a = new Helper();
a.a = 1; try{
return a;
} catch ( Exception e ) { } finally {
a.a = 2;
System.out.println("do finally");
return a;
}
} static class Helper {
int a; public String toString() {
return String.valueOf(a);
}
}
}

运行的结果:

 =============test1==================
do finally
in try
===============================
=============test1_1==================
do finally
in finally
=============================== ============test2===================
do finally
1
=============================== ============test2_1===================
do finally
2
=============================== ============test3===================
do finally
2
=============================== ============test3_1===================
do finally
2
===============================

结论:

  1. 在try catch块里return的时候,finally也会被执行。
  2. return 语句会把后面的值复制到一份用来返回,如果return的是基本类型的,finally里对变量的改动将不起效果,如果return 的是引用类型的,改动将可以起效果。
  3. finally里的return语句会把try catch块里的return语句效果给覆盖掉。

看来return语句并不一定都是函数的出口,执行return时,只是把return后面的值复制了一份到返回值变量里去了。看来最佳实践是:

  1. 最好把return放到方法尾而不要在try cath 里return
  2. 不要在try catch块和finally块里都包含return
  3. 如果在try catch块里return, 则不要在finally块里操作被return的变量

文章来自:http://www.360doc.com/content/11/0905/19/1542811_146016871.shtml

return遇到finally的更多相关文章

  1. jsp中出现onclick函数提示Cannot return from outside a function or method

    在使用Myeclipse10部署完项目后,原先不出错的项目,会有红色的叉叉,JSP页面会提示onclick函数错误 Cannot return from outside a function or m ...

  2. JavaScript中fn()和return fn()

    看博客时,注意到return的重要性 参考:http://www.cnblogs.com/raoyunxiao/p/5644032.html 看似反常的例子: var i = 0; function ...

  3. 【知识积累】try-catch-finally+return总结

    一.前言 对于找Java相关工作的读者而言,在笔试中肯定免不了遇到try-catch-finally + return的题型,需要面试这清楚返回值,这也是这篇博文产生的由来.本文将从字节码层面来解释为 ...

  4. Ajax接收不到PHP return后的结果的原因

    PHP在处理ajax返回值的时候,如果使用return如 return $result会失败,echo $result却没问题. 解释原因如下: 1.ajax请求从服务器端读取返回值,而且这些返回值必 ...

  5. oncopy="document.selection.empty()"跟oncopy="return false"什么区别?

    实现效果一样,禁止复制. 区别: oncopy="document.selection.empty()"  没禁止,只是把它复制的内容,变成空了: oncopy="ret ...

  6. 自动判断应该Ajax还是return

    起因 最近回顾以前的代码,发现一个偶尔会见到的现象.一个类里面的方法可能需要Ajax返回,也有可能需要函数return.这个现象发生在网站MVC中的 逻辑层(或模型层),示例如下.IndexCtrl是 ...

  7. jquery中ajax用return来返回值无效

    jquery中,ajax返回值,有三种写法,只有其中一种是成功的 /** * async:false,同步调用 * 返回1:2 * 失败 * 分析:ajax内部是一个或多个定义的函数,ajax中ret ...

  8. 高程(3):操作符、for、for...in循环、break/continue/return语句、函数等

    1.关系操作符 注意点:1)比较操作数是两个字符串,是比较字符串的字符编码值. 如:"a" > "b"  返回 false:"a" & ...

  9. [转载]js中return的用法

    一.返回控制与函数结果,语法为:return 表达式; 语句结束函数执行,返回调用函数,而且把表达式的值作为函数的结果 二.返回控制,无函数结果,语法为:return;  在大多数情况下,为事件处理函 ...

  10. 解决springmvc报No converter found for return value of type: class java.util.ArrayList问题

    一.背景 最近闲来无事,想自己搭建一套Spring+SpringMVC+Mybatis+Mysql的环境(搭建步骤会在以后博客中给出),结果运行程序时,适用@ResponseBody注解进行返回Lis ...

随机推荐

  1. HTML可编辑下拉框

    <div style="position:relative;">   <select style="width:120px;" onchang ...

  2. Spark的TorrentBroadcast:概念和原理

    依据Spark 1.4.1源码 SparkContext的broadcast方法 注释 可以用SparkContext将一个变量广播到所有的executor上,使得所有executor都能获取这个变量 ...

  3. Visual C++ unicode and utf8 转换

    ATL宏: USES_CONVERSION; W2A A2W CString StringUtil::UTF8_to_UNICODE(const char *utf8_string, int leng ...

  4. EOJ-1708//POJ3334

    题意: 有一个连通器,由两个漏斗组成(关于漏斗的描述见描述). 现向漏斗中注入一定量的水,问最终水的绝对位置(即y轴坐标) 思路: 总体来说分为3种情况. 1.两个漏斗可能同时装有水. 2.只可能a漏 ...

  5. ubuntu 和 win7 远程登陆 + vnc登陆

    ubuntu 和 win7 远程登陆: 第一种(通过win7自带的远程桌面来连接ubuntu) 1. windows7配置 我的电脑->属性->远程设置.-----允许远程连接 2. ub ...

  6. http://www.cnblogs.com/amboyna/archive/2008/03/08/1096024.html

    http://www.cnblogs.com/amboyna/archive/2008/03/08/1096024.html

  7. [topcoder]ZigZag

    http://community.topcoder.com/stat?c=problem_statement&pm=1259&rd=4493 动态规划题.如果不用DP,暴力的应当在2^ ...

  8. 【mysql的设计与优化专题(3)】字段类型与合理的选择字段类型

    本篇博客稍微有点长,它实际上包括两个内容:一是mysql字段类型的介绍,二是在mysql建表过程中是如何正确选择这些字段类型; 字段类型 数值 MySQL 的数值数据类型可以大致划分为两个类别,一个是 ...

  9. Android view的requestLayout()

    public void requestLayout () Since: API Level 1 Call this when something has changed which has inval ...

  10. 理解 PHP 中的 Streams

    Streams 是PHP提供的一个强有力的工具,我们常常在不经意会使用到它,如果善加利用将大大提高PHP的生产力. 驾驭Streams的强大力量后,应用程序将提升到一个新的高度. 下面是PHP手册中对 ...