System.exit(0)とSystem.exit(1)の違い【回転】

2575 ワード

1.参考文献
http://hi.baidu.com/accpzhangbo/blog/item/52aeffc683ee6ec238db4965.html
2.解析
Java.lang.Systemのソースコードを表示すると、System.exit(status)という方法の説明が表示されます.コードは次のとおりです.
  /**
     * Terminates the currently running Java Virtual Machine. The
     * argument serves as a status code; by convention, a nonzero status
     * code indicates abnormal termination.
     * <p>
     * This method calls the <code>exit</code> method in class
     * <code>Runtime</code>. This method never returns normally.
     * <p>
     * The call <code>System.exit(n)</code> is effectively equivalent to
     * the call:
     * <blockquote><pre>
     * Runtime.getRuntime().exit(n)
     * </pre></blockquote>
     *
     * @param      status   exit status.
     * @throws  SecurityException
     *        if a security manager exists and its <code>checkExit</code>
     *        method doesn't allow exit with the specified status.
     * @see        java.lang.Runtime#exit(int)
     */
    public static void exit(int status) {
    Runtime.getRuntime().exit(status);
    }


コメントでは、現在実行中のjava仮想マシンを終了するための方法であることが明らかになっています.どのようにstatusが非ゼロパラメータであるかは、正常に終了していないことを示します.
  • System.exit(0)は、仮想マシン全体のコンテンツを停止しますが、dispose()はこのウィンドウを閉じるだけですが、アプリケーションexit()全体を停止しません.いずれにしても、メモリが解放されました!つまり、JVMさえオフになっているので、メモリに何かあるはずがありません.
  • System.exit(0)は正常終了プログラムであり、System.exit(1)または非0は非正常終了プログラム
  • を表す.
  • System.exit(status)は、statusの値に関係なくプログラムを終了します.returnと比較すると、returnは上位層に戻り、System.exit(status)は最上位層に戻る
  • という違いがあります.
    3.例
    if-else判断では、プログラムが予想通りに実行され、最後にプログラムを停止する必要がある場合はSystem.exit(0)を使用しますが、System.exit(1)はcatchブロックに一般的に配置され、異常がキャプチャされた場合はプログラムを停止する必要があります.System.exit(1)を使用します.このstatus=1は、このプログラムが正常に終了していないことを示すために使用されます.