C # try catch finally

Source: Internet
Author: User
Tags finally block

A common method used by catch and finally is to obtain and use resources in the try block, handle exceptions in the catch Block, and release resources in the finally block.

Catch this will catch any exceptions.

Catch (Exception e) This will capture any exceptions. In addition, the e parameter is also provided. You can use the e parameter to obtain information about the exception when handling the exception.

Catch (Exception derived class e) This will capture exceptions defined by the derived class. For example, if I want to catch an Exception with an invalid operation, write catch (InvalidOperationException e) as follows) {....} in this way, if the exception thrown in the try statement block is InvalidOperationException, it will be transferred to this place for execution. Other exceptions will not be processed.

Catch can have multiple or none. Each catch can handle a specific exception .. . Net searches for exception handling blocks in the catch order. If the block is found, the block is processed. If the block is not found, the block is thrown to the previous level. If there is no previous level, it will be thrown to the user. At this time, if you are debugging, the program will be interrupted, and if it is a deployed program, it will be aborted.

If no catch Block exists, an exception is always thrown up the layer (if any) or the program is interrupted.

Finally can be none or only one. Whether an exception occurs or not, it always runs at the end of the exception handling structure. Even if you use return in the try block, finally always needs to be executed before the return, so that you have the opportunity to do some cleanup work at the end of exception handling. For example, close the database connection. NOTE: If there is no catch block, the finally block is required.

If you do not want to handle exceptions here, but submit them to the upper layer for processing when exceptions occur, you must perform some operations in this place no matter what exceptions occur, try finally can be used. A typical application is to perform database operations: Use the following primitive to describe it:

        

 

No matter whether an exception is thrown or where return is returned, the finally statement block is always executed, so you have the opportunity to call Close to Close the database connection (even if it is not opened or fails to be opened, close operations can always be executed) to release existing connections and resources.

Next let's look at the first small example.

      i=++

The printed result is 1.
Example 2

   Value1=    =++

The result is 2.

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.