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

Source: Internet
Author: User

1. References

Http://hi.baidu.com/accpzhangbo/blog/item/52aeffc683ee6ec238db4965.html

2. Parsing

Looking at the source code of Java.lang.System, we can find the description of the System.exit (status) method, as follows:

/** * 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&G t;.     This method never returns normally. * <p> * The call <code>system.exit (n) </code> are 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 to exit with the specified status. * @see java.lang.runtime#exit (int) */public static void exit (int status) {Runtime.getruntime (). Exit (STA    TUS); }

It is clear from the comments that this method is used to end the currently running Java Virtual machine. How status is a non-0 parameter, then indicates an abnormal exit.

    1. System.exit (0) stops all the content in your entire virtual machine, and Dispose () just closes the window, but does not stop the entire application exit (). In any case, the memory is released! Which means that even the JVM is shut down, there's nothing in the memory.
    2. System.exit (0) is a normal exit program, while system.exit (1) or not 0 indicates an abnormal exit program .
    3. System.exit (status) exits the program regardless of the status value. There are different points compared to return: Return is back to the previous level, and System.exit (status) is back to the top
3. Example

In a if-else judgment, if our program is executed according to our expectations, and finally we need to stop the program, then we use System.exit (0), and System.exit (1) is generally placed in the catch block, when the exception is caught, the program needs to be stopped, We use System.exit (1). This status=1 is used to indicate that this program is not normally exited.

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

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.