472,110 Members | 1,814 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,110 software developers and data experts.

if System.IO.StreamWriter write throws an exception, is there anyway to close the System.IO.StreamWriter object? it seems to stay open when this happens then future attempts to write to that same path fail because it says its in use by another proces

if System.IO.StreamWriter write throws an exception, is there anyway to
close the System.IO.StreamWriter object? it seems to stay open when this
happens then future attempts to write to that same path fail because it says
its in use by another process.
Nov 12 '05 #1
2 2812
Daniel wrote:
if System.IO.StreamWriter write throws an exception, is there anyway to
close the System.IO.StreamWriter object? it seems to stay open when this
happens then future attempts to write to that same path fail because it says
its in use by another process.


The finally-block has been invented for this (untested):

StreamWriter w;
try {
w = new StreamWriter(null);
// 1. an exception is thrown
}
catch {
Debug.WriteLine("!");
// 2. you handle it
}
finally {
/* 3. you clean up behind you, no matter if the exception was thrown
or not! */
if( w != null ){
w.Close();
w = null;
}
}
--
Pascal Schmitt
Nov 12 '05 #2
If my System.IO.StreamWriter Write method throws "The specified network name
is no longer available." and I try to Dispose or Close it in the finaly
clause the close or dispose method just throws "The specified network name
is no longer available." again. how to clean this up? after this the stream
writer is stuck connected until i restart my process.

"Pascal Schmitt" <ne*******@cebra.nu> wrote in message
news:en****************@TK2MSFTNGP12.phx.gbl...
Daniel wrote:
if System.IO.StreamWriter write throws an exception, is there anyway to
close the System.IO.StreamWriter object? it seems to stay open when this
happens then future attempts to write to that same path fail because it says its in use by another process.


The finally-block has been invented for this (untested):

StreamWriter w;
try {
w = new StreamWriter(null);
// 1. an exception is thrown
}
catch {
Debug.WriteLine("!");
// 2. you handle it
}
finally {
/* 3. you clean up behind you, no matter if the exception was thrown
or not! */
if( w != null ){
w.Close();
w = null;
}
}
--
Pascal Schmitt

Nov 12 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

13 posts views Thread by Stumped and Confused | last post: by
4 posts views Thread by Bhavya Shah | last post: by
9 posts views Thread by ShadowOfTheBeast | last post: by
7 posts views Thread by Mark Rae | last post: by
14 posts views Thread by Jeroen | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.