cool hit counter There is a return statement in try{}, will the code in finally{} immediately after this try be executed, when will it be executed, or will it be executed after the return?_Intefrankly

There is a return statement in try{}, will the code in finally{} immediately after this try be executed, when will it be executed, or will it be executed after the return?


That's an interesting question, and the result of my testing is that it's executed in the middle of a return.

I searched the web for some information and here is the reference code.

 1 /**
 2  * 
 3  */
 4 package com.b510.test;
 5 
 6 /**
  7 * There is a return statement in try {}, will the code in finally {} immediately after this try be executed, when will it be executed, or will it be executed after the return?
 8  * @author Hongten
 9  * @date 2013-12-10
10  */
11 public class TestC {
12 
13     @SuppressWarnings("static-access")
14     public static void main(String[] args) {
15         System.out.println(" result: " + new TestC().test());
16     }
17     
18     static int test(){
19         int i = 1;
20         try {
21             System.out.println("tryinneri : " + i);
22             return i;
23         }finally{
24             System.out.println(" Go to finally...");
25             ++i;
26             System.out.println("fianllyinneri : " + i);
27         }
28     }
29 }

Output results.

 try inside i : 1
 Go to finally...
 fianlly inside the i : 2
 Results: 1

Here is the image I grabbed while debugging.

The picture above is The program executes totry Inside the module: variable"i" the case of the value of the。

The picture above is When the program reaches the return statement in the try module, press the F5 key to go to the finally module, executefinally After the statements in the module, variable"i" A change in the value of。

The above figure shows the return to the return block when the program has finished executing the finally module.

But in the final run effect, we see a result of: 1, not what we thought: 2

Why this is the case.

The one explanation above that I think is more plausible.

Another interpretation is.

 The process of the main function calling a subfunction and getting the result is like the main function preparing an empty jar, putting the result in the jar first when the subfunction wants to return the result, and then returning the program logic to the main function. By return, I mean that the subfunction says, I'm not running anymore, you main function go ahead and run, which has little result to speak of, the result was put in the jar before that was said.

E | hongtenzone@foxmail.com B |http://www.cnblogs.com/hongten


Recommended>>
1、Vsensory Studio founder Chen Xiuchao Offline VR a foolproof arcade model or a try
2、How to build a more generic business technology architecture
3、How to bring up the program wizard option for a new win32 console application project in vs2017 under windows
4、OpenAI releases a new enhanced learning algorithm nearend policy optimization
5、Recommendations from FrameworkDesignGuidelines2ndEdition

    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号