When try catch finally has multiple return, the code is executed in this way.

Source: Internet
Author: User







Try/catch has return, finally also has return, Sun also said in finallyCodeRequired,

 

 

If return in finally is executed, will return in try/catch still be executed?

If all the return statements are executed, what is the order of execution? What is the value after the return statement actually returned?

 

To try:

Bytes ------------------------------------------------------------------------------------------------------

Public class main {

Public static void main (string... ARGs ){

System. Out. println (TCF ());

}

Public static int Tcf (){

Try {

System. Out. println ("try ");

Throw new exception ();

}

Catch (exception e ){

System. Out. println ("catch ");

Return one ();// Note the output in the One Method

}

Finally {

System. Out. println ("finally ");

Return two ();

}

}

Public static int one (){

System. Out. println ("one ");

Return 1;

}

 Public static int two (){

System. Out. println ("two ");

Return 2;

}

}

Bytes -----------------------------------------------------------------------------------

The following result is displayed.

Bytes -----------------------------------------------------------------------------------

Try

Catch

One

Finally

Two

2

 

Bytes -----------------------------------------------------------------------------------

Conclusion:

First, return one (); is executed, and then the Finally block is executed according to the syntax, so return two () is executed ();

 

 

 

If you have learned how to compile, you can understand "return one ();" as compiled into several lines:

 

1 returnvalue = one ();

2 If (no finally statement block followed)

3Where goto calls this method;

4 else

5Execute finally statement Blocks

 

 

 

After two return statements are executed, it is equivalent to assigning two values to returnvalue, so returnvalue is equal to the last value.

 

What are the return values and output results of the following code? If you understand what I mean, you can easily draw a conclusion.

Bytes -----------------------------------------------------------------------------------

Try {

Try {

Return one ();

} Finally {

Return two ();

}

} Finally {

Return three ();

}

 

 

 

 

 

 

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.