• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 492
  • Last Modified:

How to detect a pending exception in Dispose()?

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
laurima
Asked:
laurima
  • 2
  • 2
1 Solution
 
TheAvengerCommented:
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
 
gregoryyoungCommented:
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
 
laurimaAuthor Commented:
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
 
gregoryyoungCommented:
there is no way you can do that ... a using block just expands into other code as far as the compiler is concerned.
0
 
laurimaAuthor Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now