一:2种finally不会执行的情况

  a.在try语句之前就return了

  b.try语句中有System.exit();语句

二:finally语句在return执行之后,return返回之前执行

  例1:

public class FinallyTest1 {
public static void main(String[] args) {
test1();
}
public static int 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;
}
}

console:

try block
finally block
b>25, b = 100

说明:在return b += 80;后先执行finally语句

再来一个例子加强这个事实

例2

public class FinallyTest1 {
public static void main(String[] args) {
System.out.println(test11());
}
public static String test11() {
try {
System.out.println("try block");
return test12();
} finally {
System.out.println("finally block");
}
}
public static String test12() {
System.out.println("return statement");
return "after return";
}
}

console:

try block
return statement
finally block
after return

说明:先执行了return 语句中的方法,然后返回值

三:如果finally中有return语句呢?

public class FinallyTest1 {
public static void main(String[] args) {
System.out.println(test2());
}
public static int 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);
}
return 200;
}
// return b;
}
}

console:

try block
finally block
b>25, b = 100
200

说明:finally里的return直接返回了,就不管try中是否还有返回语句,这里还有个小细节需要注意,finally里加上return过后,finally外面的return b就变成不可到达语句了,也就是永远不能被执行到,所以需要注释掉否则编译器报错。

四:如果finally语句中没有return语句覆盖返回值,那么原来的返回值可能因为finally里的修改而改变也可能不变。

public class FinallyTest1 {
public static void main(String[] args) {
System.out.println(test3());
}
public static int test3() {
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);
}
b = 150;
}
return 2000;
}
}

console:

try block
finally block
b>25, b = 100
100

对比下面的这个程序

public class FinallyTest1 {
public static void main(String[] args) {
System.out.println(getMap().get("KEY").toString());
}
public static Map<String, String> getMap() {
Map<String, String> map = new HashMap<String, String>();
map.put("KEY", "INIT");
try {
map.put("KEY", "TRY");
return map;
}
catch (Exception e) {
map.put("KEY", "CATCH");
}
finally {
map.put("KEY", "FINALLY");
map = null;
}
return map;
}
}

console:

FINALLY

说明:为什么测试用例1中finally里的b = 150;并没有起到作用而测试用例2中finally的map.put("KEY", "FINALLY");起了作用而map = null;却没起作用呢?这就是Java到底是传值还是传址的问题了

    1.值传递不可以改变原变量的内容和地址;

2.引用传递不可以改变原变量的地址,但可以改变原变量的内容;

    3.注意String str = new String("good")特殊,因为String是个特殊的final类,所以每次对String的更改都会重新创建内存地址并存储(也可能是在字符串常量池中创建内存地址并存入对应的字符串内容),但是因为这里String是作为参数传递的,在方法体内会产生新的字符串而不会对方法体外的字符串产生影响。相当于值传递

 

五:是不是每次返回的一定是try中的return语句呢?那么finally外的return b不是一点作用没吗?

public class FinallyTest1 {
public static void main(String[] args) {
System.out.println(test4());
}
public static int test4() {
int b = 20;
try {
System.out.println("try block");
b = b / 0;
return b += 80;
} catch (Exception e) {
b += 15;
System.out.println("catch block");
} finally {
System.out.println("finally block");
if (b > 25) {
System.out.println("b>25, b = " + b);
}
b += 50;
}
return b;
}
}

console:

try block
catch block
finally block
b>25, b = 35
85

六:若发生异常,catch语句中的return语句和try中的return语句的情况一模一样

public class FinallyTest1 {
public static void main(String[] args) {
System.out.println(test5());
}
public static int test5() {
int b = 20;
try {
System.out.println("try block");
b = b /0;
return b += 80;
} catch (Exception e) {
System.out.println("catch block");
return b += 15;
} finally {
System.out.println("finally block");
if (b > 25) {
System.out.println("b>25, b = " + b);
}
b += 50;
}
//return b;
}
}

console:

try block
catch block
finally block
b>25, b = 35
35

总结:还是例1前面的一句话:

finally语句在return执行之后,return返回之前执行

最新文章

  1. CentOS 7 程序自启动的问题
  2. iOS 代理反向传值
  3. BZOJ 2724: [Violet 6]蒲公英
  4. 【LintCode】删除链表中的元素
  5. linux redhat6.4安装oracle11g
  6. Google protocol buffer在windows下的编译
  7. JAVA的整型与字符串相互转换
  8. PS 的参考线
  9. windows下的BT服务器搭建方案
  10. 递归查找某个目录下是否存在NOTICE文件
  11. 笔记一、初识 Javascript
  12. 利用ParameterizedType获取泛型参数类型
  13. WebGL 3D 电信机架实战之数据绑定
  14. App Store10大被拒理由
  15. excel写入操作
  16. 创建DLL动态链接库——声明导出法
  17. Android Developers:使ListView滑动流畅
  18. CMA概述
  19. 自旋锁原理及java自旋锁
  20. uva473

热门文章

  1. 测开之路二十七:Flask基础之动态路由
  2. Learning OSG programing---osgwindows
  3. Compile Linux Kernel on Ubuntu 12.04 LTS (Detailed)
  4. linux装mysql
  5. JS中常见的兼容
  6. getopts的执行过程
  7. 基于 Scrapy-redis 两种形式的分布式爬虫
  8. Python之 set的特点
  9. 入门phantomjs
  10. python 读 xlsx