finally语句用法

辨析:finally语句块一定会执行吗?

请通过 SystemExitAndFinally.java示例程序回答上述问题

 public class SystemExitAndFinally {

   public static void main(String[] args)

    {

       try{   

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

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

                //System.exit(0);       

           }

  catch(Exception e) 

             {  

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

                 System.exit(0); 

             }    

         finally

        {     

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

        }

   }

}

运行结果截图:

这时候finally语句执行。

//System.exit(0);  改为System.exit(0);  

运行截图:

 

求解答:finally语句什么时候执行?

原文地址:https://www.cnblogs.com/1995-qxl/p/4964328.html