[Java] detailed source code try_catch_finnaly statement execution

Source: Internet
Author: User

Students who have been interviewed should have a question, because most java programmers have such an interview.

The questions are as follows:

There is a return statement in try {}.
Will the code in finally {} be executed? When will it be executed?
Before or after?

........................

............


I hope you don't want to read the following explanation early.


..................


............

 

Maybe your answer is before return, but to be more detailed, it is executed in the middle of return.

View Source Code:

Package dec;

/**
*
*
* <P>
* Title: try_catch_finally test code/p>
*
* <P>
* Description: example business class
* </P>
*
* <P>
* Copyright: Copyright (c) 2012
* </P>
*
*
* @ Author dml @ 2012-12-4
* @ Version 1.0
*/
Public class TestTryCatchFinaly {

Public static void main (String [] args ){
System. out. println ("test1 returned result:" + new TestTryCatchFinaly (). test1 ());
}

Int test1 (){
Int x = 1;
Try {
System. out. println ("try print:" + x );
Return x;
// This statement fails to be compiled.
// System. out. println ("try print:" + x );
} Finally {
++ X;
System. out. println ("finally print:" + x );
}
}
}


The preceding program running result:
Try print: 1
Finally print: 2
Test1 returns the result: 1 // The running result is 1. Why? The process in which the main function calls the subfunction and obtains the result is like preparing an empty jar for the primary function. When the subfunction returns the result, it first places the result in the jar, then return the program logic to the main function. The so-called return is the sub-function. If I don't run it, you can continue to run the main function. There is no result. The result is put into the jar before you say this.


Now, execute the try statement first. When return is returned, return the result, execute the finally statement, and then execute the main function statement.


Next, let's look at this function:

Public int test2 (){
Try {
Return 1;
} Finally {
Return 2;
}
}
In the main function:

System. out. println ("test2 returned result:" + new TestTryCatchFinaly (). test2 ());

Should I print the result?
............


........................


Think about it


..................

......

The execution result is:

Test2 return result: 2

Is it a bit dizzy? Not afraid ....
Let's take a look at the explanation:

The return Statement in try calls a function before the function called in finally, that is, the return Statement is executed first,
The finally statement is executed. Therefore, the returned result is 2. Return is not to let the function return immediately, but to Return
After the statement is executed, the returned result is placed in the function stack. At this time, the function does not return immediately. It needs to execute finally
The returned result is returned.

Now you should understand it and test it:
Int test3 (){
Try {
Return func1 ();
}

Finally {
Return func2 ();
}
}

Int func1 (){
System. out. println ("func1 executed ");
Return 1;
}

Int func2 (){
System. out. println ("func2 executed ");
Return 2;
}

In the main function:
System. out. println ("test3 returned result:" + new TestTryCatchFinaly (). test3 ());

What is the execution result?
........................


............


Func1 executed
Func2 executed
Test3 return result: 2

If you still don't understand it, look at the above sentence

Now, execute the try statement first. When return is returned, return the result, execute the finally statement, and then execute the main function statement.

Better understanding
..................


............


The following two examples can be summarized as follows:

Now, execute the try statement first. When return is returned, return the result. then execute the finally statement. If return is returned, return the result (that is, the final printed result ), finally, execute the main function statement.

 

P.s. the advantage of a program is that it is determined at any time. It is not sure that only programmers are really familiar with it. Do not look at the technology too hard or simply, the right thing is more important than the right thing.


Dml@2012.12.4

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.