如何在运行时检测程序是否在抛出的异常下执行?

Zam*_*oni 4 .net c# exception

我可以在运行时在方法Helper()中检测到程序执行是抛出异常的结果吗?

注意,我的目标是避免扩展方法Helper()将异常对象作为输入参数.

public void MyFunc1()
{
  try
  {
    // some code here that eventaully throws an exception
  }
  catch( Exception ex )
  {
     Helper();
  }
}

public void MyFunc2()
{
   Helper();
}

private void Helper()
{
    // how can I check if program execution is the  
    // result of a thrown exception here.
}
Run Code Online (Sandbox Code Playgroud)

jde*_*aan 7

有涉及一个可怕的黑客Marshal.GetExceptionPointers和Marshal.GetExceptionCode不放在这里所有的平台是工作:

public static Boolean IsInException()
{
   return Marshal.GetExceptionPointers() != IntPtr.Zero ||
          Marshal.GetExceptionCode() != 0;
}
Run Code Online (Sandbox Code Playgroud)

从这个页面:http://www.codewrecks.com/blog/index.php/2008/07/25/detecting-if-finally-block-is-executing-for-an-manhandled-exception/

  • 疯狂解决方案+1/2,说它糟糕的黑客+1/2 (2认同)