M
Markus Stoeger
Hi,
I have a class similar to that:
class MyClass : IDisposable {
IDisposable obj1;
IDisposable obj2;
IDisposable obj3;
MyClass() {
obj1 = new SomeDisposableClass();
obj2 = new SomeDisposableClass();
obj3 = new SomeDisposableClass();
}
void Dispose() {
obj1.Dispose();
obj2.Dispose();
obj3.Dispose();
}
}
The problem I'm having with that is that if the Dispose method of obj2
throws an exception, obj3 will not get disposed.
How would you write the Dispose method of MyClass to correctly dispose
all three objects (without swallowing the exception)?
... something like.. uh oh..:
void Dispose() {
try {
obj1.Dispose();
}
finally {
try {
obj2.Dispose();
}
finally {
obj3.Dispose();
}
}
}
thanks,
Max
I have a class similar to that:
class MyClass : IDisposable {
IDisposable obj1;
IDisposable obj2;
IDisposable obj3;
MyClass() {
obj1 = new SomeDisposableClass();
obj2 = new SomeDisposableClass();
obj3 = new SomeDisposableClass();
}
void Dispose() {
obj1.Dispose();
obj2.Dispose();
obj3.Dispose();
}
}
The problem I'm having with that is that if the Dispose method of obj2
throws an exception, obj3 will not get disposed.
How would you write the Dispose method of MyClass to correctly dispose
all three objects (without swallowing the exception)?
... something like.. uh oh..:
void Dispose() {
try {
obj1.Dispose();
}
finally {
try {
obj2.Dispose();
}
finally {
obj3.Dispose();
}
}
}
thanks,
Max