In this article we will focus on whether a finally statement is executed if a try {} statement has a return. In fact, the JVM specification has special provisions for this situation, so I will go to the code first!

public class FinallyTest {
    public int method(a) {
        int x = 1;
        try{
            ++ x;
            return x;
        }catch(Exception e){

        }finally{
            ++ x;
        }
        return x;
    }

    public static void main(String[] args) {
        FinallyTest t = new FinallyTest();
        inty = t.method(); System.out.println(y); }}Copy the code

We have the following problems with the above code, so let’s test ourselves:

  1. If a return statement is used inside a try statement block, does the finally statement block still execute?

  2. If so, how do you implement both return and finally?

  3. What is the program output above? Why is that?

Will the finally block still execute

To that question, the answer is yes. The official Java documentation says:

The finally block always executes when the try block exits.`

We see that the descriptor uses always, that is, finally must be executed after the try execution is complete. This feature allows programmers to avoid using the return, continue, or break keyword in a try statement while ignoring closing related resources. It is always a best practice to put cleanup related resources ina finally statement block.

When you use finally to close a resource, you should avoid runtime errors in the finally statement block. You can also add judgment statements to increase program robustness:

finally {
    if(out ! =null) { 
        System.out.println("Closing PrintWriter");
        out.close(); // Do not call close() directly ina finally statement
    } else { 
        System.out.println("PrintWriter not open"); }}Copy the code

try { return } finally{}?

We know that the finally statement executes, and when we run the program on the IDE, we see that the result is 2. So why not 3?

Let’s debug:

As we can see in the figure below, the value of x in try is 2 and the return X statement in the try block is executed.

The finally statement is then executed, and x is reassigned to 3.

The try statement finally reset x=3. Why is 2 returned to the main program?

The original JVM specification explicitly states this:

If the try clause executes a return, the compiled code does the following:

1. Saves the return value (if any) in a local variable.
2. Executes a jsr to the code for the finally clause.
3. Upon return from the finally clause, returns the value saved in the local variable.
Copy the code

In the case of a return in a try, the value of the try to return is stored in a local variable. In this case, x=2 will be saved. We then execute the finally statement, which returns the value that exists in the local variable, that is, x=2.

1. One more thing to note if you also use a return statement in finally, such as return ++x. So it’s going to return 3. Because the specification states that when there is a return in both try and finally, the try return is ignored and finally return is used instead.

conclusion

Today we’ll focus on how to execute a try statement with a return in it and finally statements. Our conclusions are as follows:

  1. A return ina try will hold the value, and whatever is done to the value in the finally statement will return the value held in the try statement.
  2. When both try and finally statements have return statements, the return in the try is ignored.

This article is published by OpenWrite!

The article starts: zhuanlan.zhihu.com/lovebell

Personal public account: Technology Go

Your praise and support is the biggest motivation for the author to continue to update!