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