I'm working my way through the Head Start C# book, and I know that whenever I open a stream

Stream reader = File.Open(filename));



my stream. The book also lets you know you can use a using block

using (Stream ...) { }

and the stream will be closed at the end of the block.

So I find myself working happily through the material covering exceptions, and I know that in a try, catch, finally block, the code in 'try' is executed, and if a exception is encountered, the execution jumps to 'catch', runs, and then the code in 'finally' runs always.

If that's the case, then consider the following.

   using (Stream reader = File.OpenRead(filename) 
      // Happily reading data...
      // Oh no, an exception!
      throw new MyException();
// Exception Handling
// Runs every time

Wouldn't the stream NOT get closed after this code executes? I know think it wouldn't but I'm not 100% sure.

I'm detailed in my posting because as a newbie, my understanding of concepts may be flawed and if so, I wouldn't mind a gentle (or harsh) correction. :)

Thank you for your time.

all the objects within using block will be disposed once it exit the block so the System.IO.Stream will be disposed

If you call Close() method of the stream object that object is still instantiated and that object can still be use.

For more information you can check this articles:

commented: Perfect post, wonderfully informative. +0

I see, thank you.

I see I should perhaps consult other resources in my learning. The book I'm working through is quite clear is showing the objects being disposed at the end of the block only. Thank you for the clarification.

This is my understanding:
All objects are disposed of in the using block but not immediately, only the object/s in the using brackets are sent to the garbage collector immediately. Any other object/s that are created inside the using braces are disposed of when the .net get around to it.