System. exit (0) and System. exit (1)

Source: Internet
Author: User

System. exit (0) and System. exit (1)

public static void exit(int status)
Terminate the currently running Java virtual machine. The parameter is used as the status code. By convention, a non-0 Status Code indicates that an exception is terminated.

Call this methodRuntimeClassexitMethod. This method will never return normally.

CallSystem.exit(n)Actually, it is equivalent to calling:

 Runtime.getRuntime().exit(n)
Parameters:
status-Exit status.
Terminate the currently running Java Virtual Machine by starting the VM close sequence. This method never returns normally. A variable can be used as a status code. A non-zero status code indicates an abnormal termination.

The VM close sequence contains two phases. In the first phase, all registeredDisable hook)(If any) and allow them to run simultaneously until the end. In the second phase, ifExit terminationTo run all uncalled termination methods. Once this phase is completed, the virtual machine willPause.

If you call this method only after the VM has started its closing sequence, if you are running the closing Hook, the method will be blocked indefinitely. If you have run the close hook and enabled the on-exit finalization function, This method uses the given status code (if the status code is a non-zero value) to suspend the VM; otherwise, the VM will be blocked indefinitely.

System.exitThe method is a traditional and convenient way to call this method.

 Parameters:status-Termination status. By convention, the non-zero status code table indicates that the operation is terminated abnormally.

 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 to10      * the call:11      * <blockquote><pre>12      * Runtime.getRuntime().exit(n)13      * </pre></blockquote>14      *15      * @param      status   exit status.16      * @throws  SecurityException17      *        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     }

To enhance understanding of the example:

In an if-else judgment, if our program is executed as expected and we need to stop the program at the end, then we use System. exit (0), while System. exit (1) is usually placed in the catch Block. When an exception is caught and the program needs to be stopped, we use System. exit (1 ). This status = 1 indicates that the program exits abnormally.

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.