2

I want to do something mildly silly. In my Dispose() method for an object, I want to print a debug trace for the object, telling me all events which happened while it was alive.

But as this takes time and money, I only want to do this if Dispose() is being called because an exception was thrown.

So I would like to do

if (exceptionIsCurrentlyRaised) PrintDebugStuff();

Does .NET have such a exceptionIsCurrentlyRaised property which I can query?

Tim Lovell-Smith
  • 15,310
  • 14
  • 76
  • 93
  • I doubt that you can arbitrarily keep track of all "events" that happen to an object. If you want to keep track just put logging into the object that logs what you need every time methods are called. – Matt Phillips Jul 30 '10 at 00:22
  • I do my own logging of events that interest me, but that's not the question I'm asking. – Tim Lovell-Smith Jul 30 '10 at 00:25
  • 1
    possible duplicate of [Determine if executing in finally block due to exception being thrown](http://stackoverflow.com/questions/3301507/determine-if-executing-in-finally-block-due-to-exception-being-thrown) – Reed Copsey Jul 30 '10 at 00:54
  • Yeah and also duplicate of http://stackoverflow.com/questions/1815492/how-to-determine-whether-a-net-exception-is-being-handled which has some more interesting answers – Tim Lovell-Smith Aug 02 '10 at 20:31

3 Answers3

0

I don't know if anything like this exists as I have never seen it. But it sounds like you could just create an interface that has a single bool property. Then when you are inside the catch statement but before you call the dispose method just set the flag.

I'm guessing it can't be this easy of a solution but thought I would get some ideas started.

EDIT: Ok I also found this SO article that has a similar problem: Determine if executing in finally block due to exception being thrown

Community
  • 1
  • 1
spinon
  • 10,760
  • 5
  • 41
  • 59
0

Interesting question, but I doubt that this is possible - at least not without some major hacking using debugging or profiling APIs.

Even if you were able to call some debugging API that could give you access to the current exception inside a catch block I don't think you could get the exception inside a finally block (which is where your Dispose method would be executed). By then the exception may have been handled, so, as far as the runtime is concerned, there is no exception.

The only way I can see if doing this is to register to be notified of all exceptions since your object was constructed and from there try to figure out whether the exception was caught or not. This answer may be helpful: .NET - First chance exception listener for intensive debugging?

Community
  • 1
  • 1
EMP
  • 59,148
  • 53
  • 164
  • 220
  • 1
    If the exception was handled and not rethrown, there would indeed be no exception while the 'finally' block was running. On the other hand, a common scenario is for finally blocks to run while an exception is being thrown to an outer scope. If a finally block is trying to do something like a transaction rollback and it fails, it should throw a rollback-failure exception with the pending exception as an InnerException. Unfortunately, there's no clean way to do that. – supercat Apr 06 '11 at 20:59
0

Actually, this is something like the "IntelliTrace" feature of Visual Studio 2010, which can record what happened during a debugging session when you were not at a breakpoint.

John Saunders
  • 160,644
  • 26
  • 247
  • 397