[Interviewer] try to get there return, finally will be implemented?

In this article we explore the main if try {}statement has return, in this case finallythe statement will execute it? In fact, JVM specification is a special provision of this situation, then I tried to get the code!

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

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

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

For the above code, we have the following few questions from the test it:

  1. If you use the return statement in the try block, the finally block will then execute it?

  2. If you do, then how to achieve both the implementation and execution finally return it?

  3. What the above program outputs are? why?

finally block will execute it

For this question, the answer is yes. Java official documentation on is so described:

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

We see a description of the word used is always, that after the execution is complete try, finally will be executed . This feature allows programmers to avoid the tryuse of the statement return, continueor breakkeywords related resources while ignoring the close of operations. The clean-up resources into finallya block of statements has been a best practice.

PS: resource use finally closed when the reminder to everyone, should try to avoid finallyrun-time error occurs statement block, you can determine the appropriate sentence to be added to increase the robustness of the program:

finally {
    if (out != null) { 
        System.out.println("Closing PrintWriter");
        out.close(); // 不要在finally语句中直接调用close()
    } else { 
        System.out.println("PrintWriter not open");
    } 
}

try { return } finally{}?

We know finally statement is executed when we run the program in the IDE, you will find that running is the result of 2. So why not 3 it?

We look to debug:

We can see the figure below, try the value of x is 2, and executes the try block of return xstatements.

try the variable values

After performing finally statement, x value of 3, and finally the successful execution block return x.

finally the variable values

try a return x=2, finally returned to the statement x=3, and the statement after finally closer to return, why the result is 2 it?

The original JVM specification which clearly illustrates this situation:

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.

If the effect that in a case where the try the return, the value will first try to return to a pre-existing local variable, i.e. in this example x = 2 will be preserved. Finally statement to execute next, and finally returns the value of a local variable are present, i.e., return to x = 2.

Notes: Another point to note, if you use the return statement in finally in, such as return ++ the X-. The program return value is 3. Because the specification, when the try and finally there are return, will return try to ignore, and the use of return finally.

to sum up

Today introduces the try statement when there is a return, their implementation and finally statement. Our conclusion are:

  1. try there return, will first temporarily stored value, regardless of the value in finally statements What will we do, and ultimately returns are temporarily stored value statement try.
  2. When both the try and finally statement return statement, ignores try in return.

    This article from the blog article multiple platforms OpenWrite release!

Article starters: https://zhuanlan.zhihu.com/lovebell

Personal Public Number: Technical Go

Your point of praise and support is the greatest driving force of continuous updating!

Guess you like

Origin www.cnblogs.com/LoveBell/p/12007181.html