Java--finally

Source: Internet
Author: User
Tags finally block

The finally clause (clause) is always executed???

Package com.volshell.test; Public classMain { Public Static voidMain (string[] args) {Change1 (Ten); }    Private Static voidChange1 (intword) {System. out. println ("Test Results:"+ Test (1)); }    Private Static intTestinti) {if(i = =1)            return 0; System. out. println ("going into the try block"); Try{System. out. println ("Try block"); return 3; } Catch(Exception e) {//Todo:handle Exception}finally{System. out. println ("finally"); I++; returni; }    }}

The results above are test results: 0

First: If the try is not entered, the finally clause is not executed.

1 Package com.volshell.test;2 3  Public classMain {4      Public Static voidMain (string[] args) {5Change1 (Ten);6     }7 8     Private Static voidChange1 (intword) {9System. out. println ("Test Results:"+ Test (1));Ten     } One  A     Private Static intTestinti) { - //if (i = = 1) - //return 0; theSystem. out. println ("going into the try block"); -         Try { -System. out. println ("Try block"); -             system.exit (0); +             return 3; -}Catch(Exception e) { +             //Todo:handle Exception A}finally { atSystem. out. println ("finally"); -i++; -             returni; -         } -     } -}

Test result: Going into try Block
Try Block
This time also did not enter the finally. Because System.exit (0) is called in a try;

Second: When a thread is executing a try statement block or a catch statement block, it is suddenly interrupted or terminated, then the corresponding finally will not be executed.

***********************************************************************************************************

The finally Block
The finally block always executes when the try block exits. This ensures the finally
block is executed even if an unexpected exception occurs. But finally was useful for
More than just exception handling-it allows the programmer to avoid have cleanup
code accidentally bypassed by a return,continue, or breaks. Putting cleanup code in a
finally block is always a good practice, even when the no exceptions is anticipated.
Note:if The JVM exits while the try or catch code is being executed and then the finally
block may not execute. Likewise, if the thread executing the try or catch code is
interrupted or killed, the finally block may not execute even though the application
As a whole continues.

***************************************************************************************************************

Questions about the order of execution of try,catch,finally:

***************************************************************************************************************

Where either at least one catch clause, or the FINALLY clause, must is present. there is at least one catch or finally clause. Can have no catch

The body of the try statement is executed until either a exception is thrown or the body
Finishes successfully. The try is executed---the exception is thrown or the program executes correctly.

If An exception are thrown, each catch clause are examined in turn,
from first T o Last, to see whether the type of the exception object is assignable to
the type declared in the catch. When a assignable catch clause is found, it block
is executed with its identifier set to reference the exception Obje Ct. No other catch
clause'll be executed. Any number of catch clauses, including zero, can is associated
with a particular Try as long as each clause catches a D Ifferent type of exception.
If No appropriate catch is found, the exception percolates (penetration) out of the the Try statement
to any outer try that MIG HT has a catch clause to handle it. --if no appropriate capture is found, leave the capture outside to handle the .

If a finally clause is present with a try, it code is executed after all other processing
In the try was complete. This happens no matter what completion was achieved, whether
Normally, through an exception, or through a control flow statement such as Return or
Break. The finally all clause (including the return clause) is executed only after the statement in the try is finished (excluding the exception statement, return statement, break statement).

***************************************************************************************************************

Third: The finally all clause (including the return clause in it) is executed only after the statement in the try is finished (excluding the exception statement, return statement, break statement). After the finally is processed, it returns to the remaining clauses in the try.

Java--finally

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.