Is it possible to tell the difference between the normal exit of a using block and an exit that is due to an exception being thrown? Using try/catch/finally I can write:
Foo f = new Foo();
try
{
f.stuff();
}
catch()
{
f.ThereWasAnError();
}
finally
{
f.Dispose();
}
but it would be nice if the users of my class didn’t need to care about this, and could write
using (var f = new Foo()) { ... }
I can’t see a way to tell in the Dispose method why I’m being called. Is it possible?
I do not recommend the following. As Eric Lippert writes, this is an abuse of the disposable-pattern.
That being said, the following should work: