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语句什么时候执行?

 

posted on 2015-11-14 14:31  覃兽兽  阅读(202)  评论(0编辑  收藏  举报