Java基础之finally语句与return语句详解

作者:星光_依旧灿烂 时间:2021-11-27 19:21:22 

一、return语句执行顺序

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


package exception;

public class Demo06 {
   public static void main(String[] args) {
       System.out.println(func());
   }

public static int func(){
       int a = 10;
       try{
           System.out.println("try中的代码块");
           return a += 10;
       }catch (Exception e){
           System.out.println("catch中的代码块");
       }finally {
           System.out.println("finally中的代码块");
           if(a > 10){
               System.out.println("a > 10,"+"a="+a);
           }
       }
       return a += 50;
   }
}

运行结果:

try中的代码块
finally中的代码块
a > 10,a=20
20

注意:

a > 10,a=20的结果说明了return a += 10已经执行了,但是没有直接返回,而是先去执行finally语句的内容,然后再去返回结果

二、覆盖问题

finally块中的return语句会覆盖try块的return返回


package exception;

public class Demo07 {
   public static void main(String[] args) {
       System.out.println(func());
   }

public static int func(){
       int a = 10;
       try{
           System.out.println("try中的代码块");
           return a += 10;
       }catch (Exception e){
           System.out.println("catch中的代码块");
       }finally {
           System.out.println("finally中的代码块");
           if(a > 10){
               System.out.println("a>10,"+"a="+a);
           }
           return 100;
       }
   }
}

运行结果:

try中的代码块
finally中的代码块
a>10,a=20
100

注意:

(1)如果try中有return语句,finally中也有return语句,最终执行的是finally中的return语句
(2)如果finally代码块中写了return语句,那么finally之后的return语句就变成不可到达的语句,需要注释掉,否则编译不过

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

(1)测试1


package exception;

public class Demo08 {
   public static void main(String[] args) {
       System.out.println(func());
   }

public static int func(){
       int a = 10;
       try{
           System.out.println("try中的代码块");
           return a += 20;
       }catch (Exception e){
           e.printStackTrace();
           System.out.println("catch中的代码块");
       }finally {
           System.out.println("finally中的代码块");
           a  += 20;
           if(a > 10){
               System.out.println("a > 10,a="+a);
           }
           a += 20;
       }
       return 200;
   }
}

运行结果:

try中的代码块
finally中的代码块
a > 10,a=50
30

注意:

对于基本数据类型来说,finally中对返回值的修改不会影响try中的返回变量的值

(2)测试2


package exception;

import java.util.HashMap;
import java.util.Map;

public class Demo09 {
   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<>();
       map.put("KEY","INIT");
       try{
           map.put("KEY","try");
           return map;
       }catch (Exception e){
           e.printStackTrace();
           map.put("KEY","catch");
       }finally {
           map.put("KEY","finally");
           map = null;
       }
       return map;
   }
}

运行结果:

finally

注意:

对于引用数据类型来说,finally中对返回值的修改会影响try中的返回变量的值

三、异常情况

try块中的return语句在异常的情况下不会被执行


package exception;

public class Demo10 {
   public static void main(String[] args) {
       System.out.println(func());
   }

public static int func(){
       int a = 10;
       try{
           System.out.println("try中的代码块");
           a = a/0;
           return a += 50;
       }catch (Exception e){
           a += 15;
           System.out.println("catch中的代码块");
       }finally {
           System.out.println("finally中的代码块");
           if(a > 20){
               System.out.println("a > 20,a ="+a);
           }
           a += 10;
       }
       return a;
   }
}

运行结果:

try中的代码块
catch中的代码块
finally中的代码块
a > 20,a =25
35

注意:

try语句块中发生异常,try语句异常后的内容不会执行,return语句也不会执行,执行的是捕获到的catch语句块和finally语句块

try中发生异常时,return写在catch语句中


package exception;

public class Demo11 {
   public static void main(String[] args) {
       System.out.println(func());
   }

public static int func(){
       int a = 10;
       try{
           System.out.println("try中的代码块");
           a = a /0;
           return a += 10;
       }catch (Exception e){
           System.out.println("catch中的代码块");
           return a += 15;
       }finally {
           System.out.println("finally中的代码块");
           if (a > 10){
               System.out.println("a > 10, a = "+a);
           }
           a += 50;
           System.out.println(a);
       }
   }
}

运行结果:

try中的代码块
catch中的代码块
finally中的代码块
a > 10, a = 25
75
25

注意:

try中发生异常之后,catch中的return语句先执行,确定了返回值之后(保存起来,finally中的语句对返回值无影响)再去finally语句块,执行完之后再返回a的值,finally中对a的修改对返回值无效

四、finally语句一定会被执行吗?

(1)当程序进入try语句之前就出现异常时,会直接结束

(2)try语句块中有强制退出时也不会执行finally语句块中的代码


System.exit(0);

代码示例:


package exception;

public class Demo12 {
   public static void main(String[] args) {
       int a = 10;
       try{
           System.out.println("try block");
           System.exit(0);
       }catch (Exception e){
           System.out.println("catch block");
       }finally {
           System.out.println("finally block");
       }
   }
}

运行结果:

try block

来源:https://blog.csdn.net/qq_46804966/article/details/116138354

标签:java,finally,return,语句
0
投稿

猜你喜欢

  • Android缓存机制——LruCache的详解

    2023-07-30 07:26:34
  • 如何把spring boot项目部署到tomcat容器中

    2023-10-08 18:53:51
  • Android application捕获崩溃异常怎么办

    2023-09-26 11:07:12
  • 浅谈Java并发中的内存模型

    2022-09-07 17:54:47
  • 如何将maven源改为国内阿里云镜像

    2023-07-25 13:47:33
  • 浅谈JVM内存溢出原因和解决思路

    2023-11-23 12:24:15
  • SpringBoot2整合activiti6环境搭建过程解析

    2023-11-09 02:27:03
  • SpringBoot项目创建使用+配置文件+日志文件详解

    2023-11-20 12:49:43
  • 轻松掌握Java建造者模式

    2023-11-06 15:24:38
  • Java 数据库连接池详解及简单实例

    2023-08-10 15:54:07
  • SpringBoot详细讲解静态资源导入的实现

    2023-07-26 13:23:21
  • 10种简单的Java性能优化

    2023-06-20 20:43:41
  • Android Flutter实现搜索的三种方式详解

    2023-07-10 18:00:49
  • Java实现按权重随机数

    2023-11-28 23:15:32
  • IDEA 2020.3最新永久激活码(免费激活到 2099 年,亲测有效)

    2023-07-14 05:37:43
  • java实现简单单链表

    2023-10-30 09:45:46
  • 详解IDEA启动多个微服务的配置方法

    2023-11-24 09:22:24
  • Java 深拷贝与浅拷贝的分析

    2023-07-30 14:13:13
  • java 进制转换实例详解

    2023-07-05 11:53:45
  • Spring Bean生命周期之BeanDefinition的合并过程详解

    2023-11-29 02:50:35
  • asp之家 软件编程 m.aspxhome.com