자바 뿔 끝 [013]: finally 블록 에 있 는 코드 는 반드시 실 행 됩 니까?

3908 단어 자바jvmthreadsun
 Sun Tutorial       :The finally block always executes when the try block exits. This ensures that the finally block is executed even if an unexpected exception occurs.    finally             。

             :

 
view plaincopy to clipboardprint?
public class Test {  
    public static void main(String[] args) {  
        try {  
            System.out.println(args[0]);  
            System.out.println("I'm nomal");  
        } catch (Exception ex) {  
            System.out.println("I'm exception");  
        } finally {  
            System.out.println("I'm finally.");  
        }  
    }  
}  

        ,   ,         ,"I'm finally."         。        ,    Tutorial        。

             ,   try  catch     return  ,       ?

           :

 
view plaincopy to clipboardprint?
public class Test {  
    public static void main(String[] args) {  
        try {  
            System.out.println(args[0]);  
            System.out.println("I'm nomal");  
                        return;  
        } catch (Exception ex) {  
            System.out.println("I'm exception");  
                        return;  
        } finally {  
            System.out.println("I'm finally.");  
        }  
    }  
}  
          ,   try catch            return  。

            ?         ,      ,      "I'm finally."。          ,        ,     :

 
view plaincopy to clipboardprint?
>java Test  
I'm exception  
I'm finally.  
  
>java Test hello  
hello  
I'm nomal  
I'm finally.  

                     ,   ,finally         。     try catch   return        ?          :

 
view plaincopy to clipboardprint?
public class Test {  
    public static void main(String[] args) {  
        try {  
            System.out.println(args[0]);  
            System.out.println("I'm nomal");  
                        return;  
        } catch (Exception ex) {  
            System.out.println("I'm exception");  
                        return;  
        } finally {  
            System.out.println("I'm finally.");  
        }  
                System.out.println("Out of try.");  
    }  
}  
   try             ,    。

      ,    :

 
view plaincopy to clipboardprint?
Exception in thread "main" java.lang.Error: Unresolved compilation problem:   
    Unreachable code  

         ,  return     ,           ,     finally    。

               ,   return       ,“           ”,        , return     System.exit(),       。

 
view plaincopy to clipboardprint?
public class Test {  
    public static void main(String[] args) {  
        try {  
            System.out.println(args[0]);  
            System.out.println("I'm nomal");  
                        System.exit(0);  
        } catch (Exception ex) {  
            System.out.println("I'm exception");  
                        System.exit(0);  
        } finally {  
            System.out.println("I'm finally.");  
        }  
    }  
}  
      ,  ,"I'm finally."   。

     System.exit()         ,      API    :exit(int status): Terminates the currently running Java Virtual Machine。     ,JVM      ,       finally     。

          :

      VM    ,finally         。

좋은 웹페이지 즐겨찾기