Re: Using vs. Try Catch

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



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.

.



Relevant Pages

  • Re: multithreaded tcp/ip server large page file
    ... I did notice that but I really think this is some form of leak. ... The resources that these objects own may never be released, and the resources are left alive for the whole run of the program. ... 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. ...
    (microsoft.public.dotnet.languages.vb)
  • RE: Using vs. Try Catch
    ... block uses the resources and the Finally block disposes of them. ... If you need to handle an exception that might occur within the Using block, ... Microsoft Online Community Support ... where an initial response from the community or a Microsoft Support ...
    (microsoft.public.dotnet.languages.vb)
  • RE: Change style of scrollbars
    ... names under Microsoft Visual Studio ... CMFCVisualManagerOffice2007 class to load the XML resources from the MFC ... Microsoft Online Community Support ... initial response from the community or a Microsoft Support Engineer within ...
    (microsoft.public.vc.mfc)
  • Re: Try Finally...
    ... Now we can protect every more or less specialized action ... an error indication (exception or error code) has to be ... an according exception handler can be inserted into the code. ... resources, as they are related to specific actions. ...
    (comp.lang.pascal.delphi.misc)
  • RE: Backup & Monitoring not working!
    ... Error message in Windows Small Business Server 2003: ... Microsoft CSS Online Newsgroup Support ... This newsgroup only focuses on SBS technical issues. ... An unhandled exception occurred during the execution of the ...
    (microsoft.public.windows.server.sbs)