How do I detect when the currently-executing code is being called from within a catch block?
void SomeFunction()
{
// how do I detect whether I am being called from within a catch block?
}
EDIT:
For those asking, I wanted to implement a class like this, never mind the error bubbling logic: On writing this code sample, I'm getting a compiler error "A throw statement with no arguments is not allowed outside of a catch clause" so that kinda destroys my idea anyway.
public class ErrorManager {
public void OnException(Exception ex) {
LogException(ex);
if (IsInsideCatchBlockAlready()) {
// don't destroy the stack trace,
// but do make sure the error gets bubbled up
// through the hierarchy of components
throw;
} else {
// throw the error to make it bubble up
// through the hierarchy of components
throw ex;
}
}
void LogException(Exception ex) {
// Log the exception
}
bool IsInsideCatchBlockAlready() {
// How do I implement this?
}
}
bool throwIfNotFound
parameter or whatever it is you want to accomplish. A method should never rely on whoever calls it or how, that's what parameters are for. – Soothbool caught = true
is out of the question? – CardamomAppDomain.FirstChanceException
andAppDomain.UnhandledException
. All that come through the first, but not the last, have been caught already. – Forebodingcatch
clause. Thethrow
by itself to rethrow the exception needs to be in thecatch
block, however. – Retrogress