是否可能有一个全局异常钩子? [英] Is it possible to have a global exception hook?

查看:173
本文介绍了是否可能有一个全局异常钩子?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码很好地覆盖了异常处理(try..except)。一些例外情况预计不会发生,一些例外情况也会相当频繁地发生,这是预料之中的。现在我想为这段代码添加一些自动测试。知道在执行期间发生了多少异常是很好的,所以我可以稍后看看预期的数字是否提高或任何意外的发生。我不想用调试代码来混乱每个异常处理块,所以我的问题是:



有没有办法安装一些正确的全局异常处理程序在所有其他异常处理块之前?我正在寻找记录这些例外的中心位置。



感谢任何建议!



这很重要:它是Delphi 2009)

解决方案

您可以执行以下操作:




  • 保留System.RaiseExceptObjProc变量的值,在正常的Delphi应用程序中指向SysUtils.RaiseExceptObject

  • 创建自己的RaiseExceptObjectproc和将它分配给您自己的RaiseExceptObjectproc中的RaiseExceptObjProc变量

  • ,然后调用保存的RaiseExceptObjProc值



有关详细信息,请参阅上述变量和过程声明。


my code is fairly well covered with exception handling (try..except). Some exceptions are not expected to happen and some exceptions happen fairly often, which is expected and ok. Now I want to add some automated tests for this code. It would be good to know how many exceptions happened during execution, so I can later see if the expected number was raised or anything unexpected happened. I don't want to clutter every exception handling block with debug code, so my question is:

Is there a way to install some kind of global exception handler which sits right before all other exception handling blocks? I am searching for a central place to log these exceptions.

Thanks for any suggestions!

(And if this matters: it is Delphi 2009)

解决方案

You can do the following:

  • preserve the value of System.RaiseExceptObjProc variable, which in normal Delphi app is pointing to SysUtils.RaiseExceptObject
  • create your own "RaiseExceptObject" proc and assign it to the RaiseExceptObjProc variable
  • in your own "RaiseExceptObject" proc you can do what you want, then call saved RaiseExceptObjProc value

For details, see above variable and procedure declarations.

这篇关于是否可能有一个全局异常钩子?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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