Try catch finally and using

Source: Internet
Author: User

You should be familiar with Try catch finally. The methods you may use may be as follows:

Try catch (you can match multiple catch)

 

Try
{
}
Catch (Exception)
{
Throw;
}

 

Try finally

Try
{
}
Finally
{
}

 

Try catch finally (similarly, you can match multiple catch)

Try catch finally Try
{
}
Catch (ArgumentNullException e)
{}
Catch (Exception ex)
{}
Finally
{
}

 

Here, the function of finally is simply to say that "the Code in finally will continue to be executed no matter whether the code in try is normally executed or an exception occurs ", therefore, we usually execute some of our cleanup operations in finally. It is especially important to perform necessary cleanup operations when operating some unmanaged resources or precious resources. For details, refer to MSDN.

Let's take a look at try finally. I don't know if you use try finally or use using {}, a more concise syntax {}. For using, I do not want to explain its usage in detail here. If you want to know more, please refer to here. We all know that using is only used to make the syntax more concise. I don't know if it is appropriate to use the word "syntactic sugar" here. To verify whether try finally and using are consistent, I checked the compiled code again (here I still use the MSDN example ):

 

Code {
Font font1 = new Font ("Arial", 10.0f );
Try
{
Byte charset = font1.GdiCharSet;
}
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.