Use of Try Catch finally

Source: Internet
Author: User
Tags finally block try catch

Http://hi.baidu.com/vincentwen/blog/item/b92d0923f1e4c64793580757.html Try Catch finally 1, the code that foresees the possibility of throwing an exception is included in the TRY statement block. 2. If an exception occurs, the execution of the catch is transferred. Catch is written in several ways: catch this will catch any exceptions that occur. catch (Exception E) This will catch any exceptions that occur. In addition, the E parameter is provided, and you can use the e parameter to obtain information about the exception when handling the exception. catch (Exception derived class E) This captures the exception defined by the derived class, for example, I want to catch an exception for an invalid operation, which can be written as follows: catch (InvalidOperationException e) {...} This way, if a try statement block is thrown The exception is InvalidOperationException, which will be transferred to the place of execution, other exceptions are not handled. Catch can have more than one, or no, each catch can handle a specific exception.. NET finds exception handling blocks in the order in which you catch them, and if found, handles them and throws up a level if they are not found. If there is no previous level, then thrown to the user, at this point, if you are debugging, the program will break run, if the program is deployed, will be aborted. If there is no catch block, the exception is always thrown to the upper (if any), or the program is interrupted. 3, finally finally can not, or can only one. Whether or not an exception occurs, it always runs at the end of this exception handling structure. Even if you return with return within a try block, finally is always executed before returning, which gives you a chance to do some cleanup at the end of the exception handling. such as closing the database connection, and so on. Note: If there is no catch statement block, then the finally block is required.     If you do not want to handle the exception here, and when the exception occurs, commit to the upper layer processing, but in this place no matter what happens, you have to do something, you can use try Finally, a typical application is to do database operations: Use the following primitive to illustrate: try {     Dataconnection.open ();     Datacommand.executereader (); ... return; } finally {Dataconnection.close ()} regardless of whether or not an exception is thrown, and no matter where the return is returned, the finalThe LY statement block is always executed so that you have the opportunity to call close to close the database connection (even if it is not open or open, the shutdown is always possible), so that the resulting connection can be freed and the resource freed. By the way, return can be placed in a try statement block. But no matter when the time comes to return, the finally will be executed before returning. Summarize the code that the try {//executes, which may have an exception. Once an exception is found, jump to catch execution immediately. Otherwise, the contents of the catch will not be executed} catch {//unless the execute code in the try has an exception, the code here will not execute} finally {//whatever happens, including a return in the try catch, can be understood as long as the try or  If you catch it, you will be sure to execute finally}

  

Use of Try Catch finally

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.