Re: Using vs. Try Catch
- From: cj <cj@xxxxxxxxxxxxx>
- Date: Thu, 28 Jun 2007 08:48:13 -0400
Thank you.
Linda Liu [MSFT] wrote:
Hi Cj,.
The Using statement declares the beginning of a Using block and optionally acquires the system resources that the block controls.
A Using block behaves like a Try...Finally construction in which the Try block uses the resources and the Finally block disposes of them. Because of this, the Using block guarantees disposal of the resources, no matter how you exit the block. This is true even in the case of an unhandled exception, except for a StackOverflowException.
If you need to handle an exception that might occur within the Using block, you can add a complete Try...Finally construction to it. For example:
Using sw As New System.IO.StreamWriter(fileName, True)
Try
sw.WriteLine(strToWrite)
sw.Close() ' I think you still need to call the Close method to close the stream when you're using the Using statement
Catch ex As Exception
End Try
End Using
If there is an exception when the Using statement is acquiring a resource, i.e. in your case, sw As New System.IO.StreamWriter(fileName, True) throws an exception, CLR will catch this exception and show a default exception dialog. You could add a Try..Catch construction to the complete Using block to catch the exception occurring in the Using statement as well as within the Using block.
Try
Using sw As New System.IO.StreamWriter(filename, True)
sw.WriteLine(strToWrite)
sw.Close()
End Using
Catch ex As Exception
End Try
Hope this helps.
If you have any question, please feel free to let me know.
Sincerely,
Linda Liu
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.
Note: The MSDN Managed Newsgroup support offering is for non-urgent issues where an initial response from the community or a Microsoft Support Engineer within 1 business day is acceptable. Please note that each follow up response may take approximately 2 business days as the support professional working with you may need further investigation to reach the most efficient resolution. The offering is not appropriate for situations that require urgent, real-time or phone-based interactions or complex project analysis and dump analysis issues. Issues of this nature are best handled working with a dedicated Microsoft Support Engineer by contacting Microsoft Customer Support Services (CSS) at http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
- References:
- Using vs. Try Catch
- From: cj
- RE: Using vs. Try Catch
- From: Linda Liu [MSFT]
- Using vs. Try Catch
- Prev by Date: Anyone use Win32Shutdown on a ME pc?
- Next by Date: Re: modfy the desktopbackground
- Previous by thread: RE: Using vs. Try Catch
- Next by thread: Re: closing down an application
- Index(es):
Relevant Pages
|