Turn Try Catch finally

Source: Internet
Author: User
Tags finally block try catch

Try
Catch
Finally

1. The code that anticipates 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 (derived class E of exception)
This captures the exceptions 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 the exception thrown in the Try statement block is InvalidOperationException, it will go to that place and the other exception is 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, there can be no or 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 perform database operations:
Use the following primitive to illustrate:
Try
{
Dataconnection.open ();
Datacommand.executereader ();
...
Return
}
Finally
{
Dataconnection.close ();
}

The finally statement block will always execute, whether or not an exception is thrown, or return from any place, 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.

Summary

try {//execute code, where there may be 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 inside the try has an exception, the code here does not execute}

finally {//No matter what happens, including a return in the try catch, it is understood that if a try or catch is executed, it will be executed finally}

Origin:http://blog.sina.com.cn/s/blog_63dac1ed01013iuh.html

Turn 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.