Java中finally与return的执行顺序(很详细)

 

地址:https://blog.csdn.net/a1036645146/article/details/123717258

在 Java 的异常处理中,try、catch 和 finally 是按顺序执行的。如果 try 中没有异常,则顺序为 try→finally,如果 try 中有异常,则顺序为 try→catch→finally。但是当 try、catch、finally 中加入 return 之后,return 和 finally 的执行顺序让很多人混淆不清。下面来分别说明一下。

1. try 和 catch 中带有 return

1)try 中带有 return

  1.  
    public class tryDemo {
  2.  
    public static int show() {
  3.  
    try {
  4.  
    return 1;
  5.  
    } finally {
  6.  
    System.out.println("执行finally模块");
  7.  
    }
  8.  
    }
  9.  
    public static void main(String args[]) {
  10.  
    System.out.println(show());
  11.  
    }
  12.  
    }

输出结果如下:

执行finally模块
1

2)try 和 catch 中都带有 return 

  1.  
    public class tryDemo {
  2.  
    public static int show() {
  3.  
    try {
  4.  
    int a = 8 / 0;
  5.  
    return 1;
  6.  
    } catch (Exception e) {
  7.  
    return 2;
  8.  
    } finally {
  9.  
    System.out.println("执行finally模块");
  10.  
    }
  11.  
    }
  12.  
    public static void main(String args[]) {
  13.  
    System.out.println(show());
  14.  
    }
  15.  
    }

输出结果为:

执行finally模块
2

当 try 代码块或者 catch 代码块中有 return 时,finally 中的代码总会被执行,且 finally 语句 return 返回之前执行。

注意:可以使用编译器的 Debug 功能查看详细过程。如果不了解如何使用 Debug 功能可参考《Java Eclipse如何调试代码》一节。

2. finally 中带有 return

  1.  
    public class tryDemo {
  2.  
    public static int show() {
  3.  
    try {
  4.  
    int a = 8 / 0;
  5.  
    return 1;
  6.  
    } catch (Exception e) {
  7.  
    return 2;
  8.  
    } finally {
  9.  
    System.out.println("执行finally模块");
  10.  
    return 0;
  11.  
    }
  12.  
    }
  13.  
    public static void main(String args[]) {
  14.  
    System.out.println(show());
  15.  
    }
  16.  
    }

输出结果如下:

执行finally模块
0

当 finally 有返回值时,会直接返回该值,不会去返回 try 代码块或者 catch 代码块中的返回值。

注意:finally 代码块中最好不要包含 return 语句,否则程序会提前退出。

3. finally 中改变返回值

下面先来看 try 代码块或者 catch 代码块中的返回值是普通变量时,代码如下:

  1.  
    public class tryDemo {
  2.  
    public static int show() {
  3.  
    int result = 0;
  4.  
    try {
  5.  
    return result;
  6.  
    } finally {
  7.  
    System.out.println("执行finally模块");
  8.  
    result = 1;
  9.  
    }
  10.  
    }
  11.  
    public static void main(String args[]) {
  12.  
    System.out.println(show());
  13.  
    }
  14.  
    }

输出结果为:

执行finally模块
0

由输出结果可以看出,在 finally 代码块中改变返回值并不会改变最后返回的内容。

当返回值类型是引用类型时,结果也是一样的,代码如下:

  1.  
    public class tryDemo {
  2.  
    public static Object show() {
  3.  
    Object obj = new Object();
  4.  
    try {
  5.  
    return obj;
  6.  
    } finally {
  7.  
    System.out.println("执行finally模块");
  8.  
    obj = null;
  9.  
    }
  10.  
    }
  11.  
    public static void main(String args[]) {
  12.  
    System.out.println(show());
  13.  
    }
  14.  
    }

输出结果为:

执行finally模块
java.lang.Object@15db9742

当 try 代码块或 catch 代码块中的 return 返回值类型为普通变量或引用变量时,即使在后面 finally 代码块中对返回值的变量重新赋值,也不会影响最后返回的值。

总结为以下几条:

  • 当 try 代码块和 catch 代码块中有 return 语句时,finally 仍然会被执行。
  • 执行 try 代码块或 catch 代码块中的 return 语句之前,都会先执行 finally 语句。
  • 无论在 finally 代码块中是否修改返回值,返回值都不会改变,仍然是执行 finally 代码块之前的值。
  • finally 代码块中的 return 语句一定会执行。
posted @ 2022-08-10 10:15  双间  阅读(691)  评论(0编辑  收藏  举报