如何在运行时检测程序是否在抛出异常的情况下执行? [英] How to detect if a program is executing under a thrown exception at runtime?

查看:98
本文介绍了如何在运行时检测程序是否在抛出异常的情况下执行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Can I detect at runtime inside method Helper() that the program execution is the result of a thrown exception?

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

Note, my goal is to avoid extending method Helper() to take an exception object as a input pararmeter.

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.
}


推荐答案

有一个可怕的骇客涉及 Marshal.GetExceptionPointers Marshal.GetExceptionCode 不适用于所有平台,它是:

There is one horrible hack involving Marshal.GetExceptionPointers and Marshal.GetExceptionCode that doesn't work on all platforms here it is:

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

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

这篇关于如何在运行时检测程序是否在抛出异常的情况下执行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆