What are the best practice on handling an exception caused by a Dispose
method when its called from inside a loop? Wrap the entire loop in a
try-catch or do the try-catch on each iteration to get as many of the object
Disposed as possible?
Take the following example:
Bitmap[] bmpArray = ... // Some fancy bitmap array.
try
{
// Som bitmap operation like adding a watermark.
}
finally
{
// We need to Dispose of every bitmap in the array.
foreach(Bitmap bmp in bmpArray)
{
// Aside from the comments I think this is a pretty normal finally
construct.
if(bmp != null)
bmp.Dispose(); // Note: This line could throw a
SafeNativeMethods.StatusException!
// Now for arguments sake let's say that it's more important that as
many of the pictures are being disposed as possible than it is to make it go
quick by wrapping the entire loop in a try-catch... then what do you do?
// Do I have to wrap the Dispose call in it's own try-catch? What are
the perf implications?
}
}
Are there some common rules about Dispose/Close and exceptions getting
thrown by those two?
Thanks in advance...
--
Johannes Hansen
System Consultant, frontAvenue A/S