System的exit

System的exit

源碼

 /**
     * 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);
    }

功 能: 關閉所有文件,終止正在執行的進程。

exit(0):正常運行程序並退出程序;

exit(1):非正常運行導致退出程序;

測試方法:

 // 測試方法
    public static void main(String[] args) {

        StackType stackType = new StackType();
        try {
            DATA3 data3 = stackType.PopST(stackType); // 此處調用了 exit 直接關閉了當前現成 之後方法都未執行
        }catch (Exception ee){
            System.out.printf("sss");
        }


    }


    DATA3 PopST(StackType s){
        if(s.top==0){
            System.out.printf("棧爲空!");
            System.exit(0);
            System.out.printf("關閉後"); //永遠不會執行
        }
        return (s.data[s.top--]);
    }

控制檯輸出

Connected to the target VM, address: '127.0.0.1:52564', transport: 'socket'
Disconnected from the target VM, address: '127.0.0.1:52564', transport: 'socket'
棧爲空!
Process finished with exit code 0
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章