Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to detect a pending exception in Dispose()?

Posted on 2004-08-18
8
Medium Priority
?
456 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 2000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses
Course of the Month12 days, 20 hours left to enroll

972 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