规则:
1、不管有没有出现异常,finally块中代码都会执行;
2、当try和catch中有return时,finally仍然会执行;
3、finally是在return后面的表达式运算后执行的(此时并没有返回运算后的值,而是先把要返回的值保存起来,不管finally中的代码怎么样,返回的值都不会改变,任然是之前保存的值),所以函数返回值是在finally执行前确定的;
4、finally中最好不要包含return,否则程序会提前退出,返回值不是try或catch中保存的返回值。
整个过程是:
先执行try模块中包含的语句,如果出现异常,执行catch模块中的语句,如果有return语句,会先执行到return之前的语句(包含return语句中的表达式运算),然后执行finally模块中的语句,如果没有出现异常则值一直执行到return之前的语句(包含return语句中的表达式运算),然后执行finally模块中的语句,如果在finally模块中有return语句,那么程序就提前退出,不会再回到try模块或者catch模块中的return。但是如果finally模块中是没有return语句,那么finally模块中的语句,如果修改的是return语句中的对象。(此处要留意,如果因为return在中的表达式运算的结果已经被使用一个局部变量存储起来了,因此如果修改的是基本类型,那么你在finally中去修改了也是不会改变的,如果修改的是对象,那么局部变量中存储的是一个对象的句柄或者地址,那么此时你在finally语句中修改对象的值,想要的局部变量中的值也是被改变了的,因此我们如果要这样书写需要注意结果值)
流程图:

测试数据:
package com.dj.developmenttechniquesmall.mall_producer;
import java.util.concurrent.atomic.AtomicInteger;
public class finallyTest {
volatile static int va = 1;
public static void main(String[] args) {
int result = test();
int result1 = test1();
Integer result2 = test2();
int result3 = test3();
String result4 = test4();
Person result5 = test5();
int result6 = test6();
Person result7 = test7();
AtomicInteger result10 = test10();
System.out.println(result);
System.out.println(result1);
System.out.println(result2);
System.out.println(result3);
System.out.println(result4);
System.out.println(result5);
System.out.println(result6);
System.out.println(result7);
System.out.println(result10);
}
private static int test() {
try {
return 1;
} catch (Exception e) {
e.printStackTrace();
} finally {
return 2;
}
}
private static int test1() {
int i = 1;
try {
i = i + 1;
return i;
} catch (Exception e) {
e.printStackTrace();
} finally {
i = i + 1;
}
return i;
}
private static Integer test2() {
Integer i = 1;
try {
i = i + 1;
return i;
} catch (Exception e) {
e.printStackTrace();
} finally {
i = i + 1;
}
return i;
}
private static int test3() {
try {
va = va + 1;
return va;
} catch (Exception e) {
e.printStackTrace();
} finally {
va = va + 1;
}
return va;
}
private static String test4() {
String result = "1";
try {
result = "2";
return result;
} catch (Exception e) {
e.printStackTrace();
} finally {
result = "3";
}
return result;
}
private static Person test5() {
Person person = new Person("张飞", "男", 22);
try {
person.setAge(33);
return person;
} catch (Exception e) {
e.printStackTrace();
} finally {
person.setAge(44);
}
return person;
}
private static int test6() {
int i = 1;
try {
i++;
if (true) {
throw new RuntimeException("异常 " + i);
}
return i;
} catch (Exception e) {
i++;
e.printStackTrace();
return i;
} finally {
i++;
}
}
private static Person test7() {
Person person = new Person("张飞", "男", 22);
try {
person.setAge(33);
if (true) {
throw new RuntimeException("异常 " + person);
}
return person;
} catch (Exception e) {
person.setAge(44);
e.printStackTrace();
} finally {
person.setAge(55);
}
return person;
}
private static AtomicInteger test10() {
AtomicInteger i = new AtomicInteger(1);
try {
i.addAndGet(1);
return i;
} catch (Exception e) {
e.printStackTrace();
} finally {
i.addAndGet(1);
}
return i;
}
}
返回结果:
java.lang.RuntimeException: 异常 2
at com.dj.developmenttechniquesmall.mall_producer.finallyTest.test6(finallyTest.java:111)
at com.dj.developmenttechniquesmall.mall_producer.finallyTest.main(finallyTest.java:15)
java.lang.RuntimeException: 异常 Person(name=张飞, sex=男, age=33)
at com.dj.developmenttechniquesmall.mall_producer.finallyTest.test7(finallyTest.java:128)
at com.dj.developmenttechniquesmall.mall_producer.finallyTest.main(finallyTest.java:16)
2
2
2
2
2
Person(name=张飞, sex=男, age=44)
3
Person(name=张飞, sex=男, age=55)
1393

被折叠的 条评论
为什么被折叠?



