Java 中的System.exit


在java 中退出程序,經常會使用System.exit(1) 或 System.exit(0)。 

 

查看System.exit()方法的源碼,如下

 

 1   /**
 2      * Terminates the currently running Java Virtual Machine. The
 3      * argument serves as a status code; by convention, a nonzero status
 4      * code indicates abnormal termination.
 5      * <p>
 6      * This method calls the <code>exit</code> method in class
 7      * <code>Runtime</code>. This method never returns normally.
 8      * <p>
 9      * The call <code>System.exit(n)</code> is effectively equivalent to
10      * the call:
11      * <blockquote><pre>
12      * Runtime.getRuntime().exit(n)
13      * </pre></blockquote>
14      *
15      * @param      status   exit status.
16      * @throws  SecurityException
17      *        if a security manager exists and its <code>checkExit</code>
18      *        method doesn't allow exit with the specified status.
19      * @see        java.lang.Runtime#exit(int)
20      */
21     public static void exit(int status) {
22     Runtime.getRuntime().exit(status);
23     }

  當 status為0 時正常退出程序, 當status為非0數字時異常退出。 終止當前的Java虛擬機。 

 

System.exit()方法返回程序的最頂層, return和它相比是返回上一層。 

 

當程序執行到System.exit()方法后就會停止運行。 如果希望程序遇到System.exit后只退出當前用例,不退出當前程序,可以考慮在異常中做手腳。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM