Greetings,
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));
I ALWAYS
reader.Close();
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.
try
{
using (Stream reader = File.OpenRead(filename)
{
// Happily reading data...
// Oh no, an exception!
throw new MyException();
}
}
catch
{
// Exception Handling
}
finally
{
// 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.