查看原文
其他

Java 中的 finally 一定会被执行吗

点击关注👉 一行Java 2022-08-09

来源: 网络

一、前言

因为这次面试有问到一些同学finally的问题,发现自己这块好像有点记不太清楚了,有的点可能还给人家说错了,一度弄得场面有些尴尬。所以说这篇文章深入研究一下finally的执行情况和返回值的情况。

二、finally一定会执行吗?

先给答案:肯定不是。

我们可以看两种情况:

1.在执行try块之前直接return,我们发现finally块是不会执行的
public class TryCatchTest {

  private static int total() {
    int i = 11;
    if (i == 11) {
      return i;
    }
    try {
      System.out.println("执行try");
    } finally {
      System.out.println("执行finally");
    }
    return 0;
  }

  public static void main(String[] args) {
    System.out.println("执行main:" + total());
  }
}

输出结果:

执行main:11

复制代码

2.在执行try块之前制造一个错误,直接爆红
public class TryCatchTest {

  private static int total() {
    return 1 / 0;
    try {
      System.out.println("执行try");
    } finally {
      System.out.println("执行finally");
    }
    return 0;
  }

  public static void main(String[] args) {
    System.out.println("执行main:" + total());
  }
}

综上我们看出,如果程序连try块都执行不到,那么finally块自然就不会执行到了

不过这里有的同学就会问:如果执行了try块,finally块一定会执行吗?有的同学答案就是一定会,其实非然,看看下面的例子吧:

public class TryCatchTest {

  private static int total() {
    try {
      System.out.println("执行try");
      System.exit(0);
    } catch (Exception e) {
      System.out.println("执行catch");
    } finally {
      System.out.println("执行finally");
    }
    return 0;
  }

  public static void main(String[] args) {
    System.out.println("执行main:" + total());
  }
}

输出结果:

执行try

我们在执行try块之中退出jvm,就没事了,都不执行了。当然这个情况是比较极端的,记住就行,没事不要乱整这个。最后总结一下:不管是给try块中造了个异常,还是在try块中进行return,我们发现finally块还是会执行的。因为异常处理设计初衷就是让finally块始终执行。这个总结在finally的执行时机得到证明。

三、finally执行时机探讨

首先看常规情况:

public class TryCatchTest {

  private static int total() {
    try {
      System.out.println("执行try");
      return 11;
    } finally {
      System.out.println("执行finally");
    }
  }

  public static void main(String[] args) {
    System.out.println("执行main:" + total());
  }
}

输出结果:

执行try

执行finally

执行main:11

分析一下,不难得出在这个例子中finally块执行在try块的return之前。我们给try块中造一个异常:

public class TryCatchTest {

  private static int total() {
    try {
      System.out.println("执行try");
      return 1 / 0;
    } catch (Exception e) {
      System.out.println("执行catch");
      return 11;
    } finally {
      System.out.println("执行finally");
    }
  }

  public static void main(String[] args) {
    System.out.println("执行main:" + total());
  }
}

输出结果:

执行try

执行catch

执行finally

执行main:11

同样的,finally执行在catch块return的执行前

四、finally块中的返回值

1.finally块不含返回值,但是做改变变量值的操作

看一个例子:

public class TryCatchTest {

  private static int total() {
    int i = 0;
    try {
      System.out.println("执行try:" + i);
      return i;
    } finally {
      ++i;
      System.out.println("执行finally:" + i);
    }
  }

  public static void main(String[] args) {
    System.out.println("执行main:" + total());
  }
}

输出结果:

执行try:0

执行finally:1

执行main:0

如果看完前面分析,会发现跟想象的不太一样。我们经过前面的分析,finally块的执行时机应该是return之前,那理论上我们应该先++i使得i等于1,在执行return i; 自然会返回1。

可是结果却返回了0,这是因为Java程序会把try或者catch块中的返回值保留,也就是暂时的确认了返回值,然后再去执行finally代码块中的语句。等到finally代码块执行完毕后,如果finally块中没有返回值的话,就把之前保留的返回值返回出去。

2.finally中含有返回值

示例1:

public class TryCatchTest {

  private static int total() {
    try {
      System.out.println("执行try");
      return 1;
    } finally {
      System.out.println("执行finally");
      return 2;
    }
  }

  public static void main(String[] args) {
    System.out.println("执行main:" + total());
  }
}

输出结果:

执行try

执行finally

执行main:2


示例2:

public class TryCatchTest {

  private static int total() {
    int i = 1;
    try {
      System.out.println("执行try:" + i);
      return i;
    } finally {
      ++i;
      System.out.println("执行finally:" + i);
      return i;
    }
  }

  public static void main(String[] args) {
    System.out.println("执行main:" + total());
  }
}

输出结果:

执行try:1

执行finally:2

执行main:2


示例3:

public class TryCatchTest {

  private static int total() {
    int i = 1;
    try {
      System.out.println("执行try:" + i);
    } finally {
      ++i;
      System.out.println("执行finally:" + i);
    }
    return i;
  }

  public static void main(String[] args) {
    System.out.println("执行main:" + total());
  }
}

执行结果:

执行try:1

执行finally:2

执行main:2

这三个示例都说明了一点,在分析含有finally块的方法返回值时,要对于return出现的地方进行具体分析。在finally块中进行return操作的话,则方法整体的返回值就是finally块中的return返回值。如果在finally块之后的方法内return,则return的值就是进行完上面的操作后的return值。


END

精品资料,超赞福利,免费领


点击👇名片,关注公众号,回复【  资料  
获取大厂面试资料2T+视频教程10G+电子书
各类精品资料。

注:资料太多,截图为其中部分

最近开发整理了一个用于速刷面试题的小程序;其中收录了上千道常见面试题及答案(包含基础、并发、JVM、MySQL、Redis、Spring、SpringMVC、SpringBoot、SpringCloud、消息队列等多个类型),欢迎您的使用。QQ交流群:912509560



Java 开发必会的工具类,代码量立减90%
Java 程序员常犯的 10 个 SQL 错误
SQL语句中 left join 后用 on 还是 where?区别大了!
SpringBoot 过滤器、拦截器、监听器对比及使用场景
MyBatis-Plus 从入门到精通,这一篇就够了【推荐收藏】
Java 所有 单例模式 实现及优缺点总结
QQ和微信,总算开始互通了
面试官:if(a==1 && a==2 && a==3),有没有可能为true?

👇👇
👇点击"阅读原文",获取更多资料(持续更新中)

您可能也对以下帖子感兴趣

文章有问题?点此查看未经处理的缓存