4

If I create a stream inside of the try block and an exception is raised does the stream automatically get disposed of? For example:

try
{
   Stream stream = response.GetResponseStream();
   //Error Occurs
   stream.Close();
}
catch
{
   //Handle Error
}

If this is not the way to do it can you please suggest an approach?

Edward
  • 7,346
  • 8
  • 62
  • 123

8 Answers8

17

No, you need to use finally

Stream stream;
try
{
   stream = response.GetResponseStream();
   //Error Occurs
}
catch
{
   //Handle Error
}
finally
{
    if(stream != null)
        stream.Close();
}

Or, wrap your Stream declaration/definition in a using statement, which calls Close() automatically:

try
{
    using(Stream stream = response.GetResponseStream())
    {
        //Error happens
    }
    //stream.Dispose(), which calls stream.Close(), is called by compiler here
}
catch
{
   //Handle Error
}

Note that my two examples are not exactly equivalent - in the first, the exception is handled before steam.Close() is called, in the second the exception is handled after.

BlueRaja - Danny Pflughoeft
  • 84,206
  • 33
  • 197
  • 283
5

The stream will not automatically close in this case.

If you wanted to ensure it closes in this context you'll want to use finally to ensure closure.

Alternatively I'd wrap the whole stream in a using.

using(Steam stream = response.GetResponseStream())
{
 // Do your work here
}
Jamie Dixon
  • 53,019
  • 19
  • 125
  • 162
5

No, use a using block. It calls Dispose automatically when block finishes, even if an error occurs.

eg.

using (Stream stream = response.GetResponseStream())
{
   // do something with stream
}

It's the same as:

Stream stream = null;
try
{
    stream = response.GetResponseStream();
    // do something with stream
}
finally
{
    if (stream != null)
        stream.Dispose();
}

There are many examples on the net. A quick Google revealed the following already on stackoverflow: 'using' statement vs 'try finally'

Community
  • 1
  • 1
Will
  • 2,512
  • 14
  • 19
3
using (Stream stream = response.GetResponseStream())
{
}

Dispose() is called on scope exit. This is the correct idiom for ensuring cleanup of IDisposable objects.

Steve Townsend
  • 53,498
  • 9
  • 91
  • 140
2

using(resource) statement will take care of calling dispose.

Tomas Voracek
  • 5,886
  • 1
  • 25
  • 41
1

The answer is no. It's best to close all resources in a finally block if you have some exception handling logic around your code.

Bala R
  • 107,317
  • 23
  • 199
  • 210
1

No, to always ensure disposal, regardless of exceptions, you use:

try
{
   Stream stream = response.GetResponseStream();
   //Error Occurs
}
catch
{
   //Handle Error
}
finally
{
   if(stream!=null)
       stream.Close();
}

or simply:

using (Stream stream = response.GetResponseStream())
{   
}
Blindy
  • 65,249
  • 10
  • 91
  • 131
1

Stream implements IDisposable interface. You can use using() statements where the class implements IDisposable. This will automatically take care of closing and disposing the object without writing finally.

        try
        {
            using (var stream = response.GetResponseStream())
            {
                //Error happens
            }
        }
        catch
        {
            //Handle Error
        }
Mangesh
  • 3,987
  • 2
  • 31
  • 52