Solved

How to detect a pending exception in Dispose()?

Posted on 2004-08-18
8
380 Views
Last Modified: 2010-04-15
Is it possible to determine if an exception was thrown but is still pending?
Specifically I'm trying to do this in IDisposable.Dispose when an exception
is thrown within a using {...} block for an IDisposable object.

For example, I have a simple class that implements IDisposable, say
MyDisposable. If this class is used with the C# "using" block, Dispose is
called at the end of the block:

using (MyDisposable md = new MyDisposable())
{
    // 1. do some work with md
} // 2. md.Dispose() called implicitly here

Now, say an exception occurs at (1). The Dispose method of md will be
called, followed by any catch and finally blocks.

Given this information, is it possible to determine in the MyDisposable
implementation of Dispose if an exception was thrown while "using" the
instance in the code snippet above?

0
Comment
Question by:laurima
  • 2
  • 2
8 Comments
 
LVL 20

Expert Comment

by:TheAvenger
ID: 11830651
What if you put a try catch inside the using, like this:

using (MyDisposable md = new MyDisposable())
{
  try {
    // 1. do some work with md
  } catch {
    // Notify md somehow that an exception occured
    throw;  // rethrow the exception if you need it outside
  }
} // 2. md.Dispose() called implicitly here

Thus md will know that an exception occured before the dispose is called
0
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 11835224
well thats rather silly, you have defeat the purpose of using using ...

public class MyDispoable : IDisposable {
      public void MyMethod() {
             try {
                   //code
             }
             catch (Exception Ex) {
                    this.HadException = true;
                    throw Ex;
             }
      }

      //check variable in dispose()...
}

or just use ...

try {
      //code
      obj.Dispose();
}
catch Exception Ex {
      //doo something
      obj.Dispose()
}

this is less code than using a try catch with the using....

0
 

Author Comment

by:laurima
ID: 11873479
It also doesn't solve my problem.  I guess I need to clarify the question.  This is the situation.  I am the author of class MyDisposable, but I am not the author of the code that uses MyDisposable.  Furthermore, there is lots of code already using MyDisposable in "using" blocks.  In fact, MyDisposable doesn't work right unless it is used inside a using block, so I can pretty much assume the presence of "using".  However,  I cannot require that try/catch be added to every function that already uses MyDisposable, or even to future code that will use MyDisposable.

Inside MyDisposable.Dispose(), I want to know if an exception is "active" or "pending" (or not).  Basically I want to know whether or not Dispose() was called (i.e. the "using" block exited) due to an unhandled exception.  Putting a try/catch inside Dispose() does no good.  Also, I don't really need to know anything about the exception, just whether there is one or not.


0
 
LVL 37

Accepted Solution

by:
gregoryyoung earned 500 total points
ID: 11873551
there is no way you can do that ... a using block just expands into other code as far as the compiler is concerned.
0
 

Author Comment

by:laurima
ID: 11880978
I was hoping there was some undocumented static function/property I could call from inside Dispose() like System.Exception.PendingException() to determine if an uncought exception has been thrown.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to LOAD up a .NET Framework website with data? 2 40
Excel file not created as expected 7 48
Host asp.net pages 5 25
Exit the loop 4 34
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now