目录
try内return,finally对之前return的内容进行修改
try内return之前出现异常,finally和catch对变量作修改
try内出现异常,catch内return,finally进行各种动作
catch中抛出异常,finally 试图捕获catch的异常
返回值的执行顺序
try内return,finally不做特殊动作
-
public
class FinallyTest1 {
-
-
public static void main(String[] args) {
-
-
System.out.println(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;
-
}
-
-
}
运行结果是:
-
try block
-
finally block
-
b>25, b = 100
-
100
说明try里的return语句已经执行了,再去执行finally语句,不过并没有直接返回,而是等finally语句执行完了再返回结果。
如果觉得这个例子还不足以说明这个情况的话,下面再加个例子加强证明结论:
-
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";
-
}
-
-
}
运行结果为:
-
try block
-
return statement
-
finally block
-
after return
说明try中的return语句先执行了但并没有立即返回,先保存了after return 这个字符串,等到finally执行结束后再返回这个字符串,让main程序打印
这里大家可能会想:如果finally里也有return语句,那么是不是就直接返回了,try中的return就不能返回了?看下面。
try内return,finally也return
-
public
class FinallyTest2 {
-
-
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;
-
}
-
-
}
运行结果是:
-
try block
-
finally block
-
b>25, b = 100
-
200
这说明finally里的return直接返回了,就不管try中是否还有返回语句,这里还有个小细节需要注意,finally里加上return过后,finally外面的return b就变成不可到达语句了,也就是永远不能被执行到,所以需要注释掉否则编译器报错。
这里大家可能又想:如果finally里没有return语句,但修改了b的值,那么try中return返回的是修改后的值还是原值?看下面。
try内return,finally对之前return的内容进行修改
-
public
class FinallyTest3 {
-
-
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;
-
}
-
-
}
运行结果是:
-
try block
-
finally block
-
b>25, b = 100
-
100
相当于try 里面return 100时,将100这个值存起来了,而且不是存了b这个变量,所以对finally的修改无效,同时finally结束了,就直接返回100,没有执行return 2000
-
import java.util.*;
-
-
public
class FinallyTest6
-
{
-
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;
-
}
-
}
运行结果是:
FINALLY
相当于try 里面return map时,将map这个变量的地址存起来了,而且不是存了map这个变量,所以对finally的修改了map这个变量对应的对象的内容,map为null修改了map的地址,但无效,因为地址已经存起来了
为什么测试用例1中finally里的b = 150;并没有起到作用,而测试用例2中finally的map.put("KEY", "FINALLY");起了作用,而map = null;却没起作用呢?
这就是Java到底是传值还是传址的问题了,简单来说就是:Java中只有传值没有传址,这也是为什么map = null这句不起作用。
这同时也说明了返回语句是try中的return语句而不是 finally外面的return b;这句
不相信的话可以试下,将return b;改为return 294,对原来的结果没有一点影响。
这里大家可能又要想:是不是每次返回的一定是try中的return语句呢?那么finally外的return b不是一点作用没吗?请看下面。
try内return之前出现异常,finally和catch对变量作修改
-
public
class FinallyTest4 {
-
-
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;
-
}
-
-
}
运行结果是:
-
try block
-
catch block
-
finally block
-
b>25, b = 35
-
85
这里因 为在return之前发生了除0异常,所以try中的return不会被执行到,而是接着执行捕获异常的catch 语句和最终的finally语句,此时两者对b的修改都影响了最终的返回值,这时return b;就起到作用了。
当然如果你这里将return b改为return 300什么的,最后返回的就是300,这毋庸置疑。
这里大家可能又有疑问:如果catch中有return语句呢?当然只有在异常的情况下才有可能会执行,那么是在finally之前就返回吗?看下面。
try内出现异常,catch内return,finally进行各种动作
-
public
class FinallyTest5 {
-
-
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;
-
}
-
-
}
运行结果如下:
-
try block
-
catch block
-
finally block
-
b>25, b = 35
-
35
说明了发生异常后,catch中的return语句先执行,确定了返回值后再去执行finally块,执行完了catch再返回,finally里对b的改变对返回值无影响,原因同前面一样,也就是说情况与try中的return语句执行完全一样。
如果b=b+50后,再去return b,才会对try和catch里面返回的b的值进行修改。
return总结
最后总结:finally块的语句在try或catch中的return语句执行之后,返回之前执行。
且finally里的修改语句可能影响也可能不影响try或catch中 return已经确定的返回值(数值不影响,引用影响),若finally里也有return语句则覆盖try或catch中的return语句,直接返回。
抛出异常
catch中抛出异常,finally不做特殊动作
-
public static void main(String[] args) {
-
-
System.out.println(test());
-
}
-
-
public static String test() {
-
try {
-
System.out.println(
"try block");
-
int a =
1 /
0;
-
return
"try result";
-
}
catch (Exception e) {
-
System.out.println(
"catch block");
-
int b =
1 /
0;
-
return
"catch result";
-
-
}
finally {
-
System.out.println(
"finally block");
-
//return "finally result";
-
}
-
}
catch抛出异常后,finally继续执行,保留抛出的异常b,然后继续抛出
Exception in thread "main" java.lang.ArithmeticException: / by zero
at test.t05new.Test9.test(Test9.java:27)
at test.t05new.Test9.main(Test9.java:17)
try block
catch block
finally block
catch中抛出异常,finally return
-
public static String test() {
-
try {
-
System.out.println(
"try block");
-
int a =
1 /
0;
-
return
"try result";
-
}
catch (Exception e) {
-
System.out.println(
"catch block");
-
int b =
1 /
0;
-
return
"catch result";
-
-
}
finally {
-
System.out.println(
"finally block");
-
return
"finally result";
-
}
-
}
try block
catch block
finally block
finally result
return清除了catch里面抛出的异常
catch中抛出异常,finally 也抛出异常
-
public static void main(String[] args) {
-
-
System.out.println(test());
-
}
-
-
public static String test() {
-
try {
-
System.out.println(
"try block");
-
int a =
1 /
0;
-
return
"try result";
-
}
catch (Exception e) {
-
System.out.println(
"catch block");
-
int b =
1 /
0;
-
return
"catch result";
-
-
}
finally {
-
System.out.println(
"finally block");
-
int c =
1 /
0;
-
return
"finally result";
-
}
-
}
try block
catch block
finally block
Exception in thread "main" java.lang.ArithmeticException: / by zero
at test.t05new.Test9.test(Test9.java:32)
at test.t05new.Test9.main(Test9.java:17)
finally抛出的异常替代了catch抛出的异常
catch中抛出异常,finally 试图捕获catch的异常
-
public static void main(String[] args) {
-
-
System.out.println(test());
-
}
-
-
public static String test() {
-
try {
-
System.out.println(
"try block");
-
int a =
1 /
0;
-
return
"try result";
-
}
catch (Exception e) {
-
System.out.println(
"catch block");
-
int b =
1 /
0;
-
return
"catch result";
-
-
}
finally {
-
System.out.println(
"finally block");
-
try {
-
System.out.println(
"finally block2");
-
}
catch (Exception e2) {
-
System.out.println(
"finally block3");
-
return
"finally result";
-
}
-
//int c = 1 / 0;
-
//return "finally result";
-
}
-
}
try block
catch block
finally block
finally block2
Exception in thread "main" java.lang.ArithmeticException: / by zero
at test.t05new.Test9.test(Test9.java:27)
at test.t05new.Test9.main(Test9.java:17)
可以看到没有捕获到异常b,异常b被程序保留了,没有传递给finally
catch内抛出异常总结
catch内抛出异常,catch内流程被中断,程序内保存了这个异常,然后finally正常执行,如果内部return或者抛出异常,终止程序,然后return或者finally内异常覆盖了catch的异常。
转载:https://blog.csdn.net/xushiyu1996818/article/details/106551808