On try Catch finally and using

Source: Internet
Author: User
Tags try catch

For try Catch Finally, everyone should be familiar with the wording of your contact may be the following several types of notation:

Try catch (you can match multiple catch)

try
{
}
catch (Exception)
{
throw;
}

Try finally 
try
{
}
finally
{
}

Try Catch Finally (again, you can match multiple catch)

Try catch finally
try
{
}
catch (ArgumentNullException e)
{ }
catch (Exception ex)
{ }
finally
{
}

Here, finally the role of a simple word is "no matter whether the code in a try to execute or abnormal, will continue to execute the finally inside of the code," so we generally in the finally to perform some of our cleanup operations. Especially when it comes to manipulating unmanaged or more valuable resources, it is particularly important to perform the necessary cleanup operations, which you can refer to MSDN.

Having said that, let's look at try finally, wondering if you're using try Finally, or using the more concise syntax using {}. For using, I am not here to explain its usage in detail, if you want to know, please see here. We all know that using just to make the grammar more concise, I don't know whether to use the word grammatical sugar to describe it as appropriate. To verify that try finally and using are consistent, I looked at the compiled code again (here I use the MSDN example):

Code

{
Font font1 = new Font("Arial", 10.0f);
try
{
byte charset = font1.GdiCharSet;
}
finally
{
if (font1 != null)
((IDisposable)font1).Dispose();
}
}

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.