Groups | Blog | Home
all groups > dotnet xml > september 2005 >

dotnet xml : 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 happe


Daniel
9/7/2005 5:41:17 PM
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.

Daniel
9/7/2005 6:37:14 PM
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.

[quoted text, click to view]

Pascal Schmitt
9/8/2005 12:00:00 AM
[quoted text, click to view]

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;
}
}


--
AddThis Social Bookmark Button