Using vs. Try Catch

C

cj

my old code

Try
Dim sw As New System.io.StreamWriter(fileName, True)
sw.WriteLine(strToWrite)
sw.Close()
Catch
End Try

my new code

Using sw As New System.IO.StreamWriter(fileName, True)
sw.WriteLine(strToWrite)
End Using

If I have experience an exception in any part of the new code will using
prevent it from crashing the program? I don't think so. Do I need to
do this?

Using sw As New System.IO.StreamWriter(fileName, True)
try
sw.WriteLine(strToWrite)
catch
endtry
end using

What happens if sw As New System.IO.StreamWriter(fileName, True) throws
an exception? Can that line throw an exception?

P.S. Yes, I understand I'm just hiding the exception. That's how I need
it to work.
 
R

rowe_newsgroups

my old code

Try
Dim sw As New System.io.StreamWriter(fileName, True)
sw.WriteLine(strToWrite)
sw.Close()
Catch
End Try

my new code

Using sw As New System.IO.StreamWriter(fileName, True)
sw.WriteLine(strToWrite)
End Using

If I have experience an exception in any part of the new code will using
prevent it from crashing the program? I don't think so. Do I need to
do this?

Using sw As New System.IO.StreamWriter(fileName, True)
try
sw.WriteLine(strToWrite)
catch
endtry
end using

What happens if sw As New System.IO.StreamWriter(fileName, True) throws
an exception? Can that line throw an exception?

P.S. Yes, I understand I'm just hiding the exception. That's how I need
it to work.

If I have experience an exception in any part of the new code will using
prevent it from crashing the program?

No, using is pretty much the same as a try finally block that calls
dispose in the finally block, so it does no error handling (but will
still call dispose on an error)
What happens if sw As New System.IO.StreamWriter(fileName, True) throws
an exception? Can that line throw an exception?
Kaboom!

Do I need to do this?

Using sw As New System.IO.StreamWriter(fileName, True)
try
sw.WriteLine(strToWrite)
catch
endtry
end using

I would use the following if concerned with constructor exceptions:

Try
Using sw As New StreamWriter(fileName, True)
sw.WriteLine(strToWrite)
End Using
Catch
' Do Nothing
End Try


Thanks,

Seth Rowe
 
O

Omar Abid

my old code

Try
Dim sw As New System.io.StreamWriter(fileName, True)
sw.WriteLine(strToWrite)
sw.Close()
Catch
End Try

my new code

Using sw As New System.IO.StreamWriter(fileName, True)
sw.WriteLine(strToWrite)
End Using

If I have experience an exception in any part of the new code will using
prevent it from crashing the program? I don't think so. Do I need to
do this?

Using sw As New System.IO.StreamWriter(fileName, True)
try
sw.WriteLine(strToWrite)
catch
endtry
end using

What happens if sw As New System.IO.StreamWriter(fileName, True) throws
an exception? Can that line throw an exception?

P.S. Yes, I understand I'm just hiding the exception. That's how I need
it to work.

you code is not complete
this is complete code :
Using sw As New System.IO.StreamWriter(fileName, True)
try
sw.WriteLine(strToWrite)
catch ex as exception
msgbox ex.tostring()
endtry
end using
You can also make a spc sub for error (exc())
try
sw.WriteLine(strToWrite)
catch ex as exception
msgbox ex.tostring()
exc()
endtry
end using

Omar Abid
 
L

Linda Liu [MSFT]

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top