如何检测何时从 catch 块中调用当前执行的代码?
void SomeFunction()
{
// how do I detect whether I am being called from within a catch block?
}
编辑:
对于那些询问的人,我想实现这样的类,不要介意错误冒泡逻辑:在编写此代码示例时,我收到编译器错误“在 catch 子句之外不允许使用没有参数的 throw 语句”所以无论如何,这有点破坏了我的想法。
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?
}
}