tags:

views:

42

answers:

2

Is there a way in C# to test whether the execution of a statement happens during exception stack unwinding or not?

Thanks, Marcello.

Edit: I mean something like this:

using (NewDbTransaction()) { 
  //do some DB stuff here
} <-- here Dispose() of IDisposable is called 

void DbTransactionWrapper.Dispose() { 
  if (InException()) //is this possible???
    Rollback();
  else
    Commit();
}
A: 

Simply catch and rethrow the exception.

Edit: Or did you mean "how do I detect an exception in the finally block"? In that case You could just set a flag in the catch block and read it in the finally block.

Adrian Grigore
A: 

The stacktrace class may give you some help here, but you'd have to do a fair bit of work yourself examining the stack. I don't think there's an easy way to spot that the code is called from an exception block.

Steve Haigh