I am currently experimenting with the using statement and throwing exceptions in a constructor and think the given answer is not fully correct.
- class defintion above is not the way it should be done: finalizer/destructor missing
- description not complete: if exception is thrown in constructor and object is in using list then the following appears to happen:
- using statement terminates before entering {}
- all objects created before exception occurred (in using list) are correctly "disposed" of
- object that's constructor threw exception is left in a created but not disposed state and is at the mercy of the garbage collector to eventually get rid of it (and I think there could still be more to this if the constructor would take parameters and via those build references to itsself...)
So one should always implement a finalizer as described with IDisposable to ensure that cleanup will eventually take place (which most likely will happen at some point of time).
|