Try Catch finally in Java

Source: Internet
Author: User

In any Java textbook, exception handling is explained as a separate section, and the component is visible.

And the real can use this knowledge, only experience the development of the actual project will be a little experience.

1. Execution order of Try Catch finally

(1). General: Try----catch----finally

(2). If there is return:try----finally----return in the Try

(3). If the catch has return:try----catch----finally----return

In the latter two cases, the Reurn will be staged inside the stack until finally executes and returns.

This mechanism ensures that finally is executed once in the try catch process, regardless of the situation, and is particularly useful for freeing resources such as freeing up database links, freeing open file streams, etc.

Look at this piece of code below, the result is t will be God horse Dongdong?

 Public Static Finalstring Test () {string T= ""; Try{T= "Try"; returnT; }         Catch(Exception e) {T= "Catch"; returnT; }          finally{T= "Finally"; //return t;         }     }    

The return will be staged into the stack before we talk about finally. The T in the above code will be saved and re-enabled when the actual return, so the correct return value of the above code is "try".

But if we add a return to the finlly sentence, the T value in the previous try clause will be flooded, and the actual return value will be "finally"

2. Recommendations for use of Try Catch finally structure

It is not recommended to add return in the finally block because it will digest the exception information in the try catch, which is detrimental to the normal handling of the error and the position of the return statement:

(1). The return statement appears only once at the end of the function.
(2). The return statement appears only in the try and catch.
(3). The return statement appears only at the end of the try and function.
(4). The return statement appears only at the end of the catch and function.

Reference: http://www.cnblogs.com/aigongsi/archive/2012/04/19/2457735.html

Try Catch finally in Java

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.