欢迎访问 生活随笔!

生活随笔

当前位置: 首页 >

finally语句块一定会执行吗?

发布时间:2024/7/19 43 豆豆
生活随笔 收集整理的这篇文章主要介绍了 finally语句块一定会执行吗? 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

 

按理说finally是一定执行的,但也不排除特例,比如说以下代码:

public class SystemExitAndFinally {

    public static void main(String[] args)

    {

        try{

            System.out.println("in main");

            throw new Exception("Exception is thrown in main");

           

        }

        catch(Exception e)         {

            System.out.println(e.getMessage());

            System.exit(0);         }

        finally         {

            System.out.println("in finally");

        }

    }

}

 

它的运行结果只有

in main
Exception is thrown in main

我们发现他的finally没有执行

这是因为 System.exit(status);这个方法是用来结束当前正在运行中的java虚拟机。如何status是非零参数,那么表示是非正常退出。

转载于:https://www.cnblogs.com/hehejeson/articles/4963925.html

总结

以上是生活随笔为你收集整理的finally语句块一定会执行吗?的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。