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. spoj 390

    简单题  记得uva上有个一样的  画个图就好了 #include <cstdio> #include <cmath> const double pi = acos(-1); ...

  2. python参考手册--第3章类型和对象

    1.对象的身份.类型.值 (1)身份:对象在内存中位置的指针,地址值, >>> a = [1,2,3,4,5] >>> id(a)48497328 >> ...

  3. HDU 1757 A Simple Math Problem(矩阵快速幂)

    题目链接 题意 :给你m和k, 让你求f(k)%m.如果k<10,f(k) = k,否则 f(k) = a0 * f(k-1) + a1 * f(k-2) + a2 * f(k-3) + …… ...

  4. POJ3690+位运算

    题意:给定一个01矩阵.T个询问,每次询问大矩阵中是否存在这个特定的小矩阵. /* 64位的位运算!!! 题意: 给定一个01矩阵.T个询问,每次询问大矩阵中是否存在这个特定的小矩阵. (64位记录状 ...

  5. HDU4545+LCS

    最长公共子序列. /* LCS 最长公共子序列 */ #include<stdio.h> #include<string.h> #include<stdlib.h> ...

  6. hdu 3864 D_num

    思路:给一个数n,是否只有4个约数(包括1),也就是找3个大于1的约数. 而任何一个数都可由质数表示,所以对于给定的数,只需要进行质因数分解.这里有 2种情况:如果有3个一样的质因数,则满足条件:否则 ...

  7. 【mysql的设计与优化专题(4)】表的垂直拆分和水平拆分

    垂直拆分 垂直拆分是指数据表列的拆分,把一张列比较多的表拆分为多张表 通常我们按以下原则进行垂直拆分: 把不常用的字段单独放在一张表; 把text,blob等大字段拆分出来放在附表中; 经常组合查询的 ...

  8. java,<E>什么意思?

    泛型 就是指任意类型 比如 HashMap<K,V> 你用的时候 你可以将K,V 设置成任意类 HashMap<String,Integer> K,V 和那个E 一样 Hash ...

  9. ArcGIS学习记录—KMZ KML与SHP文件互相转换

      1.在google earth中绘制边界  工具栏中选择"Add Polygon".随意绘制一个多边形.  右击添加的图层名(左侧)保存位置为,选择保存为kmz或kml文件.  ...

  10. 174. Dungeon Game

    题目: The demons had captured the princess (P) and imprisoned her in the bottom-right corner of a dung ...