查看java.lang.System的源代碼,我們可以找到System.exit(status)這個(gè)方法的說(shuō)明,代碼如下:
[java] view plain copy/** * 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><System.exit(0)是將你的整個(gè)虛擬機(jī)里的內(nèi)容都停掉了 ,而dispose()只是關(guān)閉這個(gè)窗口,但是并沒(méi)有停止整個(gè)application exit() 。無(wú)論如何,內(nèi)存都釋放了!也就是說(shuō)連JVM都關(guān)閉了,內(nèi)存里根本不可能還有什么東西System.exit(0)是正常退出程序,而System.exit(1)或者說(shuō)非0表示非正常退出程序System.exit(status)不管status為何值都會(huì)退出程序。和return 相比有以下不同點(diǎn): return是回到上一層,而System.exit(status)是回到最上層在一個(gè)if-else判斷中,如果我們程序是按照我們預(yù)想的執(zhí)行,到最后我們需要停止程序,那么我們使用System.exit(0),而System.exit(1)一般放在catch塊中,當(dāng)捕獲到異常,需要停止程序,我們使用System.exit(1)。這個(gè)status=1是用來(lái)表示這個(gè)程序是非正常退出。
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注