[difficult Java PROBLEMS] the execution sequence of try catch finally when there is a return

There is such a problem that everyone should be familiar with exception handling, similar to the following code:

When running to line 7, an arithmetic exception will appear. The try statement block catches this exception, then starts to execute the contents in the catch statement block, and finally executes the contents in the finally statement block. Therefore, the output is as follows:

However, what happens if there is a return in the try, catch and finally statements?

We all know that the finally statement block will be executed no matter how. Who will return the return in the three blocks first? Let's test:

There is no exception in the try statement block here, so what is the execution order? Is the return in try a direct return? What should I do with a finally return? Let's look at the results first:

The result is that after the execution of the finally statement block, the A in the finally statement block is used instead of the A in the try statement block.

What if an exception occurs in the try? Let's change it:

OK, now that an arithmetic exception occurs in the try, the catch statement block will be executed, and then the finally statement block will be executed. In this case, what is the return result?

Or return 30, that is, the value of a in finally

What happens if you remove the return in finally?

The output is as follows:

Returns a in the catch statement block. Execute the code in the catch statement block first. Although the finally statement is executed, the value of a should also be changed to 30, but the actual return is 20,.

Let's do another test and comment out the return in the catch and finally statement blocks to see the return:

The output is as follows:

Therefore, although the value of a is modified in finally, the actual value returned is the value before modification. That is, the program first uses a bottle to pack the return value in the try. No matter finally, if the value of a is modified, the return value will not change, but this is only because the returned data type is the basic data type. If it is the reference type, it is still a little different. Let's take a look at chestnut.

First declare a stu class:

Test:

The output is as follows:

So you see, it is still the modified value in finally, so what is in the bottle is only the content of the variable, which can only ensure that the content will not change. If it is a reference variable, the address of the reference object is stored in the variable, and the modification of the reference object in finally will affect the returned object.

So the conclusion is actually very simple. The priority of the return of the try, catch and finally statement blocks is from low to high. First execute the statement before the return in the try. If an exception is encountered, execute the code before the return in the catch statement, and finally execute the finally statement block. If there is a return in the finally statement block, the program will return in advance. If there is no return, Returns the return in the catch statement block. If no exception is encountered, execute the finally statement block directly, and then see whether there is a return in the finally statement block to determine the returned result.

Conclusion: 1. The code in the finally block will be executed whether there is an exception or not; 2. When there is a return in try and catch, finally will still execute. The return priority in finally is greater than catch and greater than try;   3、 Finally is executed after the expression operation after return (at this time, the calculated value is not returned, but the value to be returned is saved first. Regardless of the code in finally, the returned value will not change, and it is still the previously saved value). Therefore, the function return value is determined before finally execution; 4. It is best to include return in finally, otherwise the program will exit in advance, and the return value is not saved in try or catch Return value of.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>