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
{
&