About the execution order of Try Catch finally throw return in Java

Source: Internet
Author: User
Tags throw exception try catch

try {normal statement;        1.   Exception occurred;        2.    return "Try";     } catch (Exception ex) {normal statement;        3.    return "Catch";     } finally {normal statement;        4.     return "finally"; 5. -->end}
try {        normal  statement;     //1.        exception  occurred;   //2.        return  "Try";     } catch  (Exception ex)  {    normal statement;      //3.        return  "Catch";        //5. -->End    } finally {         normal statement;     //4.     } 
try {        normal  statement;     //1.        exception  occurred;   //2.        return  "Try";     } catch  (Exception ex)  {    normal statement;      //3.        throw Exception;     } finally {        normal statement;      //4.        return  "Finally";      //5. -->end    } 
try {        normal  statement;     //1.        exception  occurred;   //2.        return  "Try";     } catch  (Exception ex)  {    normal statement;      //3.        throw Exception;     } finally {        normal statement;      //4.        throw Exception;       //5. -->end    } 
try {normal statement;        1.   Exception occurred;        2.    return "Try";     } catch (Exception ex) {normal statement;        3.      Throw Exception; 5.     -->end} finally {normal statement;    4. }


Conclusion:

1, try-catch-finally in the end will be executed, and, must take precedence over the Try/catch in the Return/throw statement execution, unless the system collapsed or the program uses System.exit (0) forcibly terminated;

2. If there is return or throw in Finally, the Return/throw in the finally is treated preferentially;

3, return and throw, from the perspective of the statement flow , the two statements are equivalent;

4. There is no return or throw in Finally, then the program goes back to Try/catch to execute the return/throw statement. If the original is catch=>finally, the Return/throw is executed back to the catch, and if it is try=>finally, the Return/throw is executed back to the try and if try/ There is no return/throw in the catch, then the try-catch-finally statement body goes out and executes the subsequent code.


This article is from the "Bitterjava" blog, make sure to keep this source http://rickqin.blog.51cto.com/1096449/1868754

About the execution order of Try Catch finally throw return in Java

Related Article

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.