views:

127

answers:

4

I open a stream and then deserialize the data in the stream. However, I added a member to the SavedEventSet object so now when I try to open an old file, it throws the exception on the deserialization line.

This is fine with me (for now) but the problem is that I handle the exception, but never close the stream (because the exception occurred before I close the stream), so when I try to open the file again, it won't let me because it is in use.

How do I close the stream after this exception? If I put stream.Close() in either the catch or the finally, it complains about trying to access an unassigned local variable. Seems like bad practice to just open a random file I know is there. Is there any way to open a stream in a way that would be similar to an empty constructor so it will look like it is assigned?

Thanks

SavedEventSet sES;
OpenFileDialog oFD = new OpenFileDialog();
Stream stream;
BinaryFormatter bF;

try
{
    oFD.InitialDirectory = this.path;
    oFD.Title = "Open Event Saved File.";
    oFD.ShowDialog();

    if(oFD.FileName.Contains(".sav"))
    {
        stream = File.Open(oFD.FileName, FileMode.Open);
        bF = new BinaryFormatter();

        sES = (SavedEventSet)bF.Deserialize(stream);
        stream.Close();

    }
}
catch (Exception ex)
{
    stream.Close();
    /*handle Exception*/
}
+4  A: 

Set stream to null before the try block.

In your catch check if stream is not null, if not then close the stream.

  SavedEventSet sES;
  OpenFileDialog oFD = new OpenFileDialog();
  Stream stream = null;
  BinaryFormatter bF;

  try
  {
    oFD.InitialDirectory = this.path;
    oFD.Title = "Open Event Saved File.";
    oFD.ShowDialog();

    if (oFD.FileName.Contains(".sav"))
    {
      stream = File.Open(oFD.FileName, FileMode.Open);
      bF = new BinaryFormatter();

      sES = (SavedEventSet)bF.Deserialize(stream);
      stream.Close();

    }
  }
  catch (Exception ex)
  {
    if (stream != null)
      stream.Close();
    /*handle Exception*/
  }
Daniel
I don't understand why it isn't just null to begin with.But that worked, thanks :)
EatATaco
+4  A: 

Use a finally block, this will execute whether an exception occurred or not:

try
{
  oFD.InitialDirectory = this.path;
  oFD.Title = "Open Event Saved File.";
  oFD.ShowDialog();

  if(oFD.FileName.Contains(".sav"))
  {
    stream = File.Open(oFD.FileName, FileMode.Open);
    bF = new BinaryFormatter();

    sES = (SavedEventSet)bF.Deserialize(stream);
  }
}
catch (Exception ex)
{
  /*handle Exception*/
}
finally
{
  if (stream != null)
    stream.Close();
}
Oded
This actually does not work. I get the same "unassigned local variable" problem.
EatATaco
I would also prefer this option, but in order for it to work, you need the stream to be crated outside the try/catch/finally block.
Wagner Silveira
@Wagner Silveira - he has created it outside the block.
Oded
Sorry, what I meant was to create and initialize it... The Stream stream = null; line that was missing in his original code.
Wagner Silveira
+16  A: 

You can use a using block, which will automatically close the stream, even if there's an exception:

using(Stream stream = File.Open(oFD.FileName, FileMode.Open))
{
    bF = new BinaryFormatter();

    sES = (SavedEventSet)bF.Deserialize(stream);
}
ShZ
+1 for `using()`. Behind the scenes it compiles down to a try/catch block, and guarantees `Dispose()` is called--it can be used on all implementers of `IDisposable`
STW
I agree, this is exactly what the 'using' block is for.
auujay
Thanks :) I didn't even know about the using block.
EatATaco
@eatataco, you might want to switch the accepted answer if this one is better...
auujay
A: 
SavedEventSet sES; 
OpenFileDialog oFD = new OpenFileDialog(); 
BinaryFormatter bF; 

try 
{ 
    oFD.InitialDirectory = this.path; 
    oFD.Title = "Open Event Saved File."; 
    oFD.ShowDialog(); 

    if(oFD.FileName.Contains(".sav")) 
    { 
        using(Stream stream = File.Open(oFD.FileName, FileMode.Open))
        {
           bF = new BinaryFormatter(); 

           sES = (SavedEventSet)bF.Deserialize(stream); 
           stream.Close(); 
        }
    } 
} 
catch (Exception ex) 
{ 
    /*handle Exception*/ 
} 
Partha Choudhury