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 termination
To 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.exit
The 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.