A Guide to System.exit() – System.exit()的指南

最后修改: 2019年 8月 26日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

In this tutorial, we’ll have a look at what System.exit means in Java.

在本教程中,我们将了解一下System.exit在Java中的含义。

We’ll see its purposes, where to use and how to use it. We’ll also see what’s the difference in invoking it with different status codes.

我们将看到它的用途,在哪里使用以及如何使用它。我们还将看到在不同的状态代码下调用它有什么区别。

2. What Is System.exit?

2.什么是System.exit?

System.exit is a void method. It takes an exit code, which it passes on to the calling script or program.

System.exit是一个void方法。它接收一个退出代码,并将其传递给调用的脚本或程序。

Exiting with a code of zero means a normal exit:

代码为0的退出意味着正常退出:

System.exit(0);

We can pass any integer as an argument to the method. A non-zero status code is considered as an abnormal exit.

我们可以传递任何整数作为方法的参数。非零状态代码被认为是异常退出。

Calling the System.exit method terminates the currently running JVM and exits the program. This method does not return normally.

调用System.exit方法可以终止当前运行的JVM并退出程序。这个方法不会正常返回。

This means that the subsequent code after the System.exit is effectively unreachable and yet, the compiler does not know about it.

这意味着System.exit之后的后续代码实际上是无法到达的 然而,编译器却不知道它。

System.exit(0);
System.out.println("This line is unreachable");

It’s not a good idea to shut down a program with System.exit(0). It gives us the same result of exiting from the main method and also stops the subsequent lines from executing, also the thread invoking System.exit blocks until the JVM terminates. If a shutdown hook submits a task to this thread, it leads to a deadlock.

System.exit(0)关闭程序并不是一个好主意。它给我们带来了从main方法中退出的相同结果,同时也停止了后续行的执行,此外,调用System.exit的线程也会阻塞,直到JVM终止。如果一个关机钩子向这个线程提交任务,就会导致死锁。

3. Why Do We Need It?

3.我们为什么需要它?

The typical use-case for System.exit is when there is an abnormal condition and we need to exit the program immediately.

System.exit的典型用例是当出现异常情况,我们需要立即退出程序。

Also, if we have to terminate the program from a place other than the main method, System.exit is one way of achieving it.

另外,如果我们必须从main方法以外的地方终止程序,System.exit是实现这一目的的一种方法。

4. When Do We Need It?

4.我们何时需要它?

It’s common for a script to rely on the exit codes of commands it invokes. If such a command is a Java application, then System.exit is handy for sending this exit code.

脚本依赖它所调用的命令的退出代码是很常见的。如果这样的命令是一个Java应用程序,那么System.exit就可以方便地发送这个退出代码。

For example, instead of throwing an exception, we can return an abnormal exit code that can then be interpreted by the calling script.

例如,我们可以不抛出一个异常,而是返回一个异常的退出代码,然后由调用脚本来解释。

Or, we can use System.exit to invoke any shutdown hooks we’ve registered. These hooks can be set to clean up the resources held and exit safely from other non-daemon threads.

或者,我们可以使用System.exit来调用我们所注册的任何关闭钩子。这些钩子可以被设置为清理所持有的资源,并从其他非daemon线程安全退出。

5. A Simple Example

5.一个简单的例子

In this example, we try to read a file and if it exists, we print a line from it. If the file does not exist, we exit the program with System.exit from the catch block.

在这个例子中,我们尝试读取一个文件,如果该文件存在,我们就打印其中的一行。如果文件不存在,我们用System.exit从catch块退出程序。

try {
    BufferedReader br = new BufferedReader(new FileReader("file.txt"));
    System.out.println(br.readLine());
    br.close();
} catch (IOException e) {
    System.exit(2);
} finally {
    System.out.println("Exiting the program");
}

Here, we must note that the finally block does not get executed if the file is not found. Because the System.exit on the catch blocks exits the JVM and does not allow the finally block to execute.

在这里,我们必须注意,如果没有找到文件,final块不会被执行。因为catch块上的System.exit会退出JVM,不允许finally块执行。

6. Choosing a Status Code

6.选择一个状态代码

We can pass any integer as a status code but, the general practice is that a System.exit with status code 0 is normal and others are abnormal exits.

我们可以传递任何整数作为状态代码,但是,一般的做法是,状态代码为0的System.exit是正常的,其他的是异常的退出。

Note that this is only a “good practice” and is not a strict rule that the compiler would care about.

请注意,这只是一种 “良好做法”,并不是编译器会关心的严格规则。

Also, it’s worth noting when we invoke a Java program from the command-line that the status code is taken into account.

此外,值得注意的是,当我们从命令行调用一个Java程序时,状态代码会被考虑在内。

In the below example, when we try to execute SystemExitExample.class, if it exits the JVM by calling the System.exit with a non-zero status code, then the following echo does not get printed.

在下面的例子中,当我们试图执行SystemExitExample.class时,如果它通过调用System.exit以非零状态代码退出JVM,那么下面的回声就不会被打印。

java SystemExitExample && echo "I will not be printed"

To make our program able to communicate with other standard tools, we might consider following the standard codes that the related systems use to communicate.

为了使我们的程序能够与其他标准工具通信,我们可以考虑遵循相关系统用来通信的标准代码。

Exit Codes With Special Meanings document prepared by The Linux Documentation Project presents a list of reserved codes.  It also advises on what codes to use for specific scenarios.

具有特殊含义的退出代码文件由Linux文档项目编写,提供了一个保留代码的列表。 它还建议在特定情况下使用哪些代码。

7. Conclusion

7.结论

In this tutorial, we discussed how System.exit works when to use it, and how to use it.

在本教程中,我们讨论了System.exit如何工作,何时使用,以及如何使用。

It’s a good practice to use exception handling or plain return statements to exit a program when working with application servers and other regular applications. Usage of System.exit method suit better for script-based applications or wherever the status codes are interpreted.

在使用应用程序服务器和其他常规应用程序时,使用异常处理或简单的返回语句来退出程序是一个好的做法。System.exit方法的使用更适合于基于脚本的应用程序或解释状态代码的地方。

You can check out the examples provided in this article over on GitHub.

你可以查看本文在GitHub上提供的例子