我如何在全球'捕获对象实例中引发的异常 [英] How do I 'globally' catch exceptions thrown in object instances

查看:69
本文介绍了我如何在全球'捕获对象实例中引发的异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在编写Winforms应用程序(C#).

I am currently writing a winforms application (C#).

按照我所看到的相当标准的方法,我正在使用企业库异常处理块.IE:在Program.cs的Main方法中,我已将事件处理程序连接到Application.ThreadException事件等.

I am making use of the Enterprise Library Exception Handling Block, following a fairly standard approach from what I can see. IE : In the Main method of Program.cs I have wired up event handler to Application.ThreadException event etc.

这种方法效果很好,可以处理应用程序的特殊情况.

This approach works well and handles the applications exceptional circumstances.

在我的一个业务对象中,我在对象属性之一的Set访问器中抛出了各种异常

In one of my business objects I throw various exceptions in the Set accessor of one of the objects properties

set {

   if (value > MaximumTrim)
    throw new CustomExceptions.InvalidTrimValue("The value of the minimum trim...");

   if (!availableSubMasterWidthSatisfiesAllPatterns(value))
        throw new CustomExceptions.InvalidTrimValue("Another message...");

   _minimumTrim = value;
}

我对这种方法的逻辑(不将其转变为何时引发异常"的讨论)仅仅是因为业务对象负责检查业务规则约束并引发可能冒泡并根据需要捕获的异常.应该注意的是,在我的应用程序的UI中,我确实检查了公共属性的设置值(并在其中显示友好对话框等操作),但抛出异常也涉及了我的业务对象的情况.可能不被UI使用,例如:例如,属性是由另一个业务对象设置的.无论如何,我想大家都明白.

My logic for this approach (without turning this into a 'when to throw exceptions' discussion) is simply that the business objects are responsible for checking business rule constraints and throwing an exception that can bubble up and be caught as required. It should be noted that in the UI of my application I do explictly check the values that the public property is being set to (and take action there displaying friendly dialog etc) but with throwing the exception I am also covering the situation where my business object may not be used by a UI eg : the Property is being set by another business object for example. Anyway I think you all get the idea.

我的问题是,连接到Application.ThreadException的处理程序未捕获这些异常,我不明白为什么.

My issue is that these exceptions are not being caught by the handler wired up to Application.ThreadException and I don't understand why.

从其他方面看,我已经完成了Application.ThreadException事件,它的处理程序"...捕获了在主GUI线程上发生的任何异常".我的业务对象中引发的异常不在此线程中吗?我尚未创建任何新线程.

From other reading I have done the Application.ThreadException event and it handler "... catches any exception that occurs on the main GUI thread". Are the exceptions being raised in my business object not in this thread? I have not created any new threads.

如果我按如下方式更新代码,则显式调用连接到Application.ThreadException的事件处理程序,便可以使用该方法.这是企业库样本中概述的方法.但是这种方法要求我将所有抛出的异常包装在try catch中,这是我试图通过使用'global'处理程序开始避免的.

I can get the approach to work if I update the code as follows, explicity calling the event handler that is wired to Application.ThreadException. This is the approach outlined in Enterprise Library samples. However this approach requires me to wrap any exceptions thrown in a try catch, something I was trying to avoid by using a 'global' handler to start with.

try
{
    if (value > MaximumTrim)
        throw new CustomExceptions.InvalidTrimValue("The value of the minimum...");

    if (!availableSubMasterWidthSatisfiesAllPatterns(value))
        throw new CustomExceptions.InvalidTrimValue("Another message");

    _minimumTrim = value;
}
catch (Exception ex)
{
    Program.ThreadExceptionHandler.ProcessUnhandledException(ex);
}

我还研究了如何将处理程序连接到AppDomain.UnhandledException事件,但这也无法捕获异常.

I have also investigated using wiring a handler up to AppDomain.UnhandledException event but this does not catch the exceptions either.

如果有人可以向我解释为什么我的异常为什么在第一个代码示例中没有被全局异常处理程序捕获的话,我会很好.是否有其他需要的方法或者我是否需要根据需要将代码包装在try catch中,如上所示?

I would be good if someone could explain to me why my exceptions are not being caught by my global exception handler in the first code sample. Is there another approach I am missing or am I stuck with wrapping code in try catch, shown above, as required?

推荐答案

如果您尝试使用useApplication.ThreadException或者AppDomain.CurrentDomain.UnhandledException调试器捕获异常!

If you try to use use Application.ThreadException or AppDomain.CurrentDomain.UnhandledException the Debugger will catch the exception!

要测试这些方法,您必须在没有调试器的情况下启动应用程序.

To test these methods you have to start the appication without a debugger.

这篇关于我如何在全球'捕获对象实例中引发的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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