Try catch finally execution order with return (most persuasive summary)

Source: Internet
Author: User
Tags finally block try catch


Conclusion:
1, regardless of the occurrence of wood anomalies, finally block code will be implemented;
2. Finally still executes when there is return in try and catch;
3, finally is after the return of the expression after the operation (at this time, not returning the value of the operation, but first to return the value to save, pipe finally in the code, the return of the value will not change, is still the value of the previous saved), Therefore, the function return value is determined before finally execution;
4, finally, it is best not to include return, otherwise the program will quit early, the return value is not a try or catch in the returned value saved.
Example:
Condition 1:try{} catch () {}finally{} return;
Obviously the program executes sequentially.
Condition 2:try{return;} catch () {} finally{} return;
The program executes the code in the try block before return (including the expression operation in the return statement);
Then execute the finally block, and finally execute the try return;
The statement return after the finally block, because the program has been return in a try and is no longer executing.
Condition 3:try{} catch () {return;} finally{} return;
The program executes a try first, and if an exception is encountered to execute a catch block,
Exception: Executes the code in the catch before return (including the expression operation in the return statement), and executes all the code in the finally statement.
The last execution of the catch block is return. Finally, the code at 4 is no longer executing.
No exception: Execute try and finally return.
Condition 4:try{return;} catch () {} Finally{return;}
The program executes the code in the try block before return (including the expression operation in the return statement);
The finally block is executed again because there is a return in the finally block and exits early.
Condition 5:try{} catch () {return;} Finally{return;}
The program executes the code in the Catch block before return, including an expression operation in the return statement;
The finally block is executed again because there is a return in the finally block and exits early.
Condition 6:try{return;} catch () {return;} Finally{return;}
The program executes the code in the try block before return (including the expression operation in the return statement);
Exception: Executes the code in the Catch block before return (including the expression operation in the return statement);
The finally block is executed again because there is a return in the finally block and exits prematurely.
No exception: Then the finally block is executed because there is a return in the finally block and exits early.

Final Conclusion: A finally statement is executed before any return statement in a try or catch is executed, if finally exists.
If finally there is a return statement, then the program will return, so finally in the return is bound to be return,
The compiler implements the finally return to a warning.

Here's a test program
. public class Finallytest  
{public
	static void Main (string[] args) {
		 
		System.out.println (new Finallytest (). Test ());;
	}

	static int test ()
	{
		int x = 1;
		Try
		{
			x + +;
			return x;
		}
		Finally
		{
++x
}}} the result is 2. 
Analysis:
	In the try statement, when the return statement is executed, the result returned is ready, and at this point the program is transferred to finally execution.
before you turn, try to place the result you want to return to a local variable that is different from X, and then, after you finish finally, remove the return result from it,
so even if the variable x is changed in Finally, it does not affect the return result.
It should use the stack to save the return value.

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.