Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to detect a pending exception in Dispose()?

Posted on 2004-08-18
8
Medium Priority
?
445 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

715 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