Guide to the Java finally Keyword – Java最后的关键词指南

最后修改: 2020年 1月 1日

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

1. Overview

1.概述

In this tutorial, we’ll explore the finally keyword in Java. We’ll see how to use it alongside try/catch blocks in error handling. Though finally is intended to guarantee the execution of code, we’ll discuss exceptional situations in which the JVM does not execute it.

在本教程中,我们将探讨Java中的finally关键字。我们将看到如何在错误处理中与try/catch块一起使用它。尽管finally旨在保证代码的执行,但我们将讨论JVM不执行它的特殊情况。

We’ll also discuss some common pitfalls where a finally block can have an unexpected outcome.

我们还将讨论一些常见的陷阱,在这些陷阱中,finally块会产生意想不到的结果。

2. What Is finally?

2.什么是最终?

finally defines a block of code we use along with the try keyword. It defines code that’s always run after the try and any catch block, before the method is completed.

finally定义了一个代码块,我们与try关键字一起使用。它定义的代码总是在try和任何catch块之后、方法完成之前运行。

The finally block executes regardless of whether an exception is thrown or caught.

无论是否抛出或捕获异常,finally块都会执行

2.1. A Quick Example

2.1.一个快速的例子

Let’s look at finally in a try-catch-finally block:

让我们看看finallytry-catch-finally块中的情况。

try {
    System.out.println("The count is " + Integer.parseInt(count));
} catch (NumberFormatException e) {
    System.out.println("No count");
} finally {
    System.out.println("In finally");
}

In this example, regardless of the value of the parameter count, the JVM executes the finally block and prints “In finally”.

在这个例子中,无论参数count的值如何,JVM都会执行finally块,并打印“In finally”

2.2. Using finally Without a catch Block

2.2.在没有catch块的情况下使用finally

Also, we can use a finally block with a try block regardless of whether a catch block is present:

另外,我们可以使用finally块与try块,而不管是否有catch块存在

try {
    System.out.println("Inside try");
} finally {
    System.out.println("Inside finally");
}

And we’ll get the output:

我们会得到输出。

Inside try
Inside finally

2.3. Why finally Is Useful

2.3.为什么finally是有用的?

We generally use the finally block to execute clean up code like closing connections, closing files, or freeing up threads, as it executes regardless of an exception.

我们通常使用finally块来执行清理代码,如关闭连接、关闭文件或释放线程,因为它的执行与异常无关。

Note: try-with-resources can also be used to close resources instead of a finally block.

注意:try-with-resources也可用于关闭资源,而不是finally块。

3. When finally Is Executed

3.当finally被执行时

Let’s have a look at all the permutations of when the JVM executes finally blocks, so we can understand it better.

让我们看看JVM执行finally块时的所有变化,以便我们更好地理解它。

3.1. No Exception Is Thrown

3.1.没有抛出异常

When the try block completes, the finally block is executed, even if there was no exception:

try块完成时,finally块被执行,即使没有出现异常。

try {
    System.out.println("Inside try");
} finally {
    System.out.println("Inside finally");
}

In this example, we aren’t throwing an exception from the try block. Thus, the JVM executes all code in both the try and finally blocks.

在这个例子中,我们并没有从try块中抛出一个异常。因此,JVM执行了tryfinally块中的所有代码。

This outputs:

这个产出。

Inside try
Inside finally

3.2. Exception Is Thrown and Not Handled

3.2.异常被抛出且未被处理

If there’s an exception and it is not caught, the finally block is still executed:

如果有一个异常并且没有被捕获,finally块仍然被执行。

try {
    System.out.println("Inside try");
    throw new Exception();
} finally {
    System.out.println("Inside finally");
}

The JVM executes the finally block even in the case of an unhandled exception.

JVM会执行 finally块,即使是在未处理的异常情况下。

And the output would be:

而输出将是。

Inside try
Inside finally
Exception in thread "main" java.lang.Exception

3.3. Exception Is Thrown and Handled

3.3.抛出和处理异常

If there’s an exception and it is caught by the catch block, the finally block is still executed:

如果有一个异常并且被catch块捕获,finally块仍然被执行。

try {
    System.out.println("Inside try");
    throw new Exception();
} catch (Exception e) {
    System.out.println("Inside catch");
} finally {
    System.out.println("Inside finally");
}

In this case, the catch block handles the thrown exception, and then the JVM executes the finally block and produces the output:

在这种情况下,catch块处理抛出的异常,然后JVM执行finally块并产生输出。

Inside try
Inside catch
Inside finally

3.4. Method Returns from try Block

3.4.来自try块的方法返回

Even returning from the method will not prevent finally blocks from running:

即使从该方法返回也不会阻止finally块的运行。

try {
    System.out.println("Inside try");
    return "from try";
} finally {
    System.out.println("Inside finally");
}

Here, even though the method has a return statement, the JVM executes the finally block before handing the control over to the calling method.

在这里,尽管该方法有一个return语句,JVM在将控制权移交给调用方法之前执行了finally块。

We’ll get the output:

我们会得到输出。

Inside try
Inside finally

3.5. Method Returns from catch Block

3.5.来自catch块的方法返回

When the catch block contains a return statement, the finally block is still called:

catch块包含一个return语句时,finally块仍然被调用。

try {
    System.out.println("Inside try");
    throw new Exception();
} catch (Exception e) {
    System.out.println("Inside catch");
    return "from catch";
} finally {
    System.out.println("Inside finally");
}

When we throw an exception from the try block, the catch block handles the exception. Though there is a return statement in the catch block, the JVM executes the finally block before handing control over to the calling method, and it outputs:

当我们从try块抛出一个异常时,catch块会处理这个异常。虽然在catch块中有一个返回语句,但在将控制权移交给调用方法之前,JVM会执行finally块,并且会输出。

Inside try
Inside catch
Inside finally

4. When finally Isn’t Executed

4.当最终没有被执行时

Although we always expect the JVM to execute the statements inside a finally block, there are some situations where the JVM will not execute a finally block.

尽管我们总是期望JVM执行finally块内的语句,但在某些情况下,JVM不会执行finally块。

We might already expect that if the operating system stops our program, then the program would not get the chance to execute all of its code. There are also some actions we can take that will similarly prevent the execution of a pending finally block.

我们可能已经想到,如果操作系统停止我们的程序,那么程序将没有机会执行所有的代码。还有一些我们可以采取的行动,同样会阻止一个待定的finally块的执行。

4.1. Invoking System.exit

4.1.调用System.exit

In this case, we are terminating the JVM by calling System.exit and hence the JVM will not execute our finally block:

在这种情况下,我们通过调用System.exit来终止JVM,因此JVM将不会执行我们的finally块。

try {
    System.out.println("Inside try");
    System.exit(1);
} finally {
    System.out.println("Inside finally");
}

This outputs:

这个产出。

Inside try

4.2. Invoking halt

4.2.调用halt

Similar to System.exit, a call to Runtime.halt also halts  the execution and the JVM does not execute any finally blocks:

System.exit类似,对Runtime.halt的调用也会停止执行,JVM不会执行任何finally块。

try {
    System.out.println("Inside try");
    Runtime.getRuntime().halt(1);
} finally {
    System.out.println("Inside finally");
}

Thus, the output will be:

因此,输出将是。

Inside try

4.3. Daemon Thread

4.3.守护神线程

If a Daemon thread enters the execution of a try/finally block and all other non-daemon threads exit before the daemon thread executes the finally block, the JVM doesn’t wait for the daemon thread to finish the execution of finally block:

如果一个daemon线程进入try/finally块的执行,并且所有其他非daemon线程在daemon线程执行finally块之前退出,JVM不会等待daemon线程完成finally块的执行。

Runnable runnable = () -> {
    try {
        System.out.println("Inside try");
    } finally {
        try {
            Thread.sleep(1000);
            System.out.println("Inside finally");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
};
Thread regular = new Thread(runnable);
Thread daemon = new Thread(runnable);
daemon.setDaemon(true);
regular.start();
Thread.sleep(300);
daemon.start();

In this example, the runnable prints “Inside try” as soon as it enters the method and waits for 1 second before printing “Inside finally”.

在这个例子中,runnable一进入方法就打印“Inside try”,等待1秒后再打印“Inside finally”

Here, we start the regular thread and the daemon thread with a small delay. When the regular thread executes the finally block, the daemon thread is still waiting within the try block. As the regular thread completes execution and exits, the JVM also exits and does not wait for the daemon thread to complete the finally block.

在这里,我们启动了regular线程和daemon线程,并有一个小的延迟。当regular 线程执行finally块时,daemon 线程仍在try块中等待。当regular 线程完成执行并退出时,JVM也退出了,并且不等待daemon 线程完成finally块。

Here’s the output:

下面是输出结果。

Inside try
Inside try
Inside finally

4.4. JVM Reaches an Infinite Loop

4.4 JVM达到了一个无限循环

Here’s a try block which contains an infinite while loop:

这里有一个try块,它包含一个无限的while循环。

try {
    System.out.println("Inside try");
    while (true) {
    }
} finally {
    System.out.println("Inside finally");
}

Though it’s not specific to finally, it’s worth mentioning that if the try or catch block contains an infinite loop, the JVM will never reach any block beyond that loop.

虽然这不是针对finally的,但值得一提的是,如果trycatch块包含一个无限循环,JVM将永远不会到达该循环之外的任何块。

5. Common Pitfalls

5.常见的陷阱

There are some common pitfalls that we must avoid when we use the finally block.

当我们使用finally块时,有一些常见的陷阱,我们必须避免。

Although it’s perfectly legal, it’s considered bad practice to have a return statement or throw an exception from a finally block, and we should avoid it at all costs.

尽管这是完全合法的,finally块中出现return语句或抛出异常被认为是不好的做法,我们应该不惜一切代价避免这种做法。

5.1. Disregards Exception

5.1.弃权例外

A return statement in the finally block ignores an uncaught exception:

finally块中的return语句会忽略一个未捕获的异常。

try {
    System.out.println("Inside try");
    throw new RuntimeException();
} finally {
    System.out.println("Inside finally");
    return "from finally";
}

In this case, the method ignores the RuntimeException thrown and returns the value “from finally”.

在这种情况下,该方法忽略了抛出的RuntimeException,并返回“from finally”值。

5.2. Ignores Other return Statements

5.2.忽略其他return语句

A return statement in the finally block ignores any other return statement in the try or catch block. Only the return statement in the finally block executes:

finally块中的return语句会忽略trycatch块中的任何其他返回语句。只有finally块中的return语句会被执行。

try {
    System.out.println("Inside try");
    return "from try";
} finally {
    System.out.println("Inside finally");
    return "from finally";
}

In this example, the method always returns “from finally” and completely ignores the return statement in the try block. This could be a very difficult bug to spot, which is why we should avoid using return in finally blocks.

在这个例子中,该方法总是返回“from finally”,完全忽略了try块中的return语句。这可能是一个非常难以发现的错误,这就是为什么我们应该避免在finally块中使用return

5.3. Changes What’s Thrown or Returned

5.3.改变被抛出或返回的内容

Also, in the case of throwing an exception from a finally block, the method disregards the exception thrown or return statements in the try and catch blocks:

另外,在从finally块抛出异常的情况下,该方法不考虑抛出的异常或return块中的trycatch语句。

try {
    System.out.println("Inside try");
    return "from try";
} finally {
    throw new RuntimeException();
}

This method never returns a value and always throws a RuntimeException.

这个方法从不返回一个值,并且总是抛出一个RuntimeException

While we may not intentionally throw an exception from the finally block as in this example, we may still encounter this issue. It can occur when cleanup methods we use in a finally block throw an exception.

虽然我们可能不会像本例中那样故意从finally块中抛出一个异常,但我们仍然可能遇到这个问题。当我们在finally块中使用的清理方法抛出一个异常时,就会出现这种情况。

6. Conclusion

6.结语

In this article, we discussed what finally blocks do in Java and how to use them. Then, we looked at different cases where the JVM executes them, and a few when it might not.

在这篇文章中,我们讨论了finally块在Java中的作用以及如何使用它们。然后,我们看了JVM执行它们的不同情况,以及一些可能不执行的情况。

Lastly, we looked at some common pitfalls associated with using finally blocks.

最后,我们看了一些与使用finally块有关的常见陷阱。

As always, the source code used in this tutorial is available over on GitHub.

一如既往,本教程中所使用的源代码可在GitHub上获得