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();
}
}