473,324 Members | 2,548 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

Exception handling in IDisposable.Dispose

Joe
Reposting here as there were no useful replies in the dotnet.framework NG...

What is the correct pattern for handling exceptions in IDisposable.Dispose,
especially in a class that manages multiple unmanaged resources? An example
of such a class is System.ComponentModel.Container.

I have always understood that the IDisposable contract was that Dispose
guarantees to release unmanaged resources owned by the object - even if the
Dispose method throws an exception.

This seems to be underlined by the .NET 2.0 implementation of StreamWriter.

The StreamWriter Dispose method attempts to flush the stream before closing
it: this can of course throw an exception (e.g. the stream may represent a
network file).

In .NET 1.1, an exception thrown while Dispose is flushing the stream is
propagated to the caller and the stream is not closed. The unmanaged
resource is not released.

This has been fixed in .NET 2.0: a try/finally block is used to ensure the
stream is always closed.

The fact that this was fixed seems to imply that Microsoft considers the
correct behavior is to release unmanaged resources whether or not there is an
exception.

What if a class, like System.ComponentModel.Container contains multiple
unmanaged resources?

Using Reflector on System.ComponentModel.Container shows it contains code
something like the following:

....
while (siteCount > 0)
{
ISite site = sites[siteCount-1];
site.Component.Site = null;
site.Component.Dispose();
}
....

This means if one of the components has a Dispose method which throws an
exception, then Dispose will not be called on the remaining components.
Which appears to break the IDispose.Dispose contract.

The only way I can see to guarantee that all resources are disposed would be
to use try/catch blocks - which signficiantly complicates the Dispose
pattern. In the case of Container, it could be something like:

....
Exception savedException = null;
while (siteCount > 0)
{
try
{
ISite site = sites[siteCount-1];
site.Component.Site = null;
site.Component.Dispose();
}
catch(Exception ex)
{
if (savedException == null) savedException = ex;
}
}
if (savedException == null)
{
// Wrap savedException in a new exception and throw it.
// Or better, save all exceptions above in a collection and add
// the collection to the new custom exception.
...
}
....


Expand AllCollapse All

Nov 16 '05 #1
0 2007

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Emil Astrom | last post by:
Hi! I wonder if there's a way to retrieve information about thrown, not yet handled exceptions. My situation is similar to the code below: class MySession : IDisposable { : : Dispose()
40
by: Kevin Yu | last post by:
is it a bad programming design to throw exception in the try block then catch it??
3
by: Giovanni Bassi | last post by:
Hello Group, I am running an operation in a different thread. There are resources that are released when the thread is done running. This is done at the end of the execution as it raises an...
4
by: Helge Jensen | last post by:
In C# 2.0 System.IO.Stream is declared as: public class Stream: ..., IDisposable { ... public void Dispose(); public void Dispose(bool); IDisposable.Dispose(); } Which must be a...
5
by: Markus Stoeger | last post by:
Hi, I have a class similar to that: class MyClass : IDisposable { IDisposable obj1; IDisposable obj2; IDisposable obj3; MyClass() {
2
by: Richard Collette | last post by:
Hi, I have a service, that runs perfectly when executed outside of the web service environment. When called as a web service I get the exception listed below sporadically. A call to the web...
23
by: TarheelsFan | last post by:
What happens whenever you throw an exception from within a constructor? Does the object just not get instantiated? Thanks for replies.
2
by: pedrito | last post by:
I've got an app that downloads from several concurrent threads. Occasionally, I get an expcetion trying to read the HttpWebResponse stream... Sorry, the code isn't short, but I figured best to...
7
by: Ralf Jansen | last post by:
For logging purposes i want to determine if the current executing code is running in an ~exceptionhandling context~. I need no details about the exception just if an exception has been thrown and...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.