Solved

How to detect a pending exception in Dispose()?

Posted on 2004-08-18
8
370 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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Launch Process with NOT as Administrator 15 67
Image(2) 3 28
Chat Room 1 30
index Out OF Range Exception error 4 32
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…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

759 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