Execution of the return statement contained in the try catch finally statement in Java (Summary)

Source: Internet
Author: User

 

 

InHereI can see try> but it is certain that the content in the finally block will be executed before the return statement in try. If there is a return statement in the finall statement block, then return directly from finally, which is also the reason that return is not recommended in finally. Let's take a look at these situations.

 

Case 1 (return in try and no return in finally ):

 

public class TryTest{public static void main(String[] args){System.out.println(test());}private static int test(){int num = 10;try{System.out.println(try);return num += 80;}catch(Exception e){System.out.println(error);}finally{if (num > 20){System.out.println(num>20 :  + num);}System.out.println(finally);}return num;}}
The output result is as follows:

Try
Num> 20: 90
Finally
90

Analysis: Obviously, "return num + = 80" is split into two statements: "num = num + 80" and "return num, run the "num = num + 80" statement in try, save it, and run the finally statement before "return num" in try, then return 90.

 

Case 2 (return exists in both try and finally ):

 

public class TryTest{public static void main(String[] args){System.out.println(test());}private static int test(){int num = 10;try{System.out.println(try);return num += 80;}catch(Exception e){System.out.println(error);}finally{if (num > 20){System.out.println(num>20 :  + num);}System.out.println(finally);return 100;}}}
The output result is as follows:

 

Try
Num> 20: 90
Finally
100

Analysis: The return Statement in try is also split. The return statement in finally is executed before the return statement in try, so the return statement in try is overwritten and no longer executed.

 

Case 3 (changing the return value num in finally ):

 

public class TryTest{public static void main(String[] args){System.out.println(test());}private static int test(){int num = 10;try{System.out.println(try);return num;}catch(Exception e){System.out.println(error);}finally{if (num > 20){System.out.println(num>20 :  + num);}System.out.println(finally);num = 100;}return num;}}
The output result is as follows:

 

Try
Finally
10

Analysis: although the return value num is changed in finally, because finally does not return the value of this num, after the finally statement is executed, test () the function will get the value of num returned by try, while the value of num in try is still the value reserved before the program enters the finally code block, so the returned value is 10.

 

But let's look at the following situation (wrap the num value in the Num class ):

 

public class TryTest{public static void main(String[] args){System.out.println(test().num);}private static Num test(){Num number = new Num();try{System.out.println(try);return number;}catch(Exception e){System.out.println(error);}finally{if (number.num > 20){System.out.println(number.num>20 :  + number.num);}System.out.println(finally);number.num = 100;}return number;}}class Num{public int num = 10;}
The output result is as follows:

 

Try
Finally
100

It can be seen from the results that the return value of num is also changed in finally. In case 3, the return (test () method in try does not get 100 ), but here it is returned by the return Statement in try.

 

 

To analyze the above situation, you need to go deep into the operation on the CLR stack during program execution in the JVM Virtual Machine. For details, referHttp://www.bkjia.com/kf/201010/76754.htmlIn this article, you can also refer to deep-dive Java Virtual Machine: Advanced JVM features and best practices.

For situations that contain return statements, we can simply summarize them as follows:

Before a try statement is returned, all other operations are completed, the values to be returned are retained, and then transferred to the statements in the finally statement execution, which are divided into the following three situations:

Scenario 1: if there is a return statement in finally, the return Statement in try will overwrite it. directly execute the return statement in finally to obtain the return value, in this way, the returned values reserved before try cannot be obtained.

Scenario 2: If there is no return statement in finally and the return value is not changed, after the finally statement is executed, the return Statement in try is executed to return the previously reserved value.

Case 3: If there is no return statement in finally, but the value to be returned is changed, it is similar to the difference between passing the reference value and passing the value. There are two situations ,:

1) if the return data is of the basic data type, the change to the basic data in finally does not work, and the return Statement in try will still return the value retained before entering the finally block.

2) If the return data is of the reference data type and the finally changes the attribute value of the referenced data type, the return Statement in try returns the value of this attribute changed in finally.

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.