打开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);
}
可以看到实际上是调用了 Runtime.getRuntime().exit(status)
来执行。那么System.exit(0)
和System.exit(1)
有什么区别呢?
答案是:System.exit(0)
是正常退出程序,而System.exit(1)
表示非正常退出。可以仔细阅读方法上面的英文注释。
公众号ID:longjiazuoA

未经允许不得转载:人生设计师 » System.exit(0)和System.exit(1)的区别