如何“全局”捕获对象实例中抛出的异常 [英] How do I 'globally' catch exceptions thrown in object instances

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

问题描述

我正在编写一个winforms应用程序(C#)。



我正在使用企业库异常处理块,遵循一个相当标准的方法,从我可以看到。 IE:在Program.cs的Main方法中,我已经将事件处理程序连接到Application.ThreadException事件等。



这种方法可以很好地处理应用程序的异常情况。 / p>

在我的一个业务对象中,我在其中一个对象属性的设置访问器中抛出了各种异常。

  set {

if(value> MaximumTrim)
throw new CustomExceptions.InvalidTrimValue(最小修剪的值);

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

_minimumTrim = value;
}

我的这种方法的逻辑(不要把这转变成一个'什么时候抛出异常讨论)只是业务对象负责检查业务规则约束,并引发可以冒泡并根据需要被捕获的异常。应该注意的是,在我的应用程序的UI中,我明确地检查了公共属性被设置的值(并且在那里显示友好的对话框等等),但是抛出异常,我也覆盖了我的业务对象可能不被UI使用,例如:该属性正由另一个业务对象设置。无论如何,我认为你们都得到了这个想法。



我的问题是这些异常没有被接收到Application.ThreadException的处理程序所捕获,我不明白为什么



从其他阅读我已经完成了Application.ThreadException事件,它处理程序...捕获在主GUI线程发生的任何异常。在我的业务对象中不是在这个线程中引发的异常?我没有创建任何新的线程。



如果我更新代码如下,我可以使该方法工作,显式调用连接到Application.ThreadException的事件处理程序。这是企业库样本中概述的方法。 然而,这种方法需要我包装try catch中抛出的异常,我试图避免使用'全局'处理程序开始。

  try 
{
if(value> MaximumTrim)
throw new CustomExceptions.InvalidTrimValue(最小值... );

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

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

我还调查了使用布线一个处理程序到AppDomain.UnhandledException事件,但这也没有捕获例外。



如果有人可以向我解释为什么我的异常在第一个代码示例中没有被我的全局异常处理程序所捕获,那将是很好的。有没有另外一种方法,我错过了,或者我按照需要在上面显示的try catch中包含代码?

解决方案

如果您尝试使用
Application.ThreadException

AppDomain.CurrentDomain.UnhandledException
调试器捕获异常!



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


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

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.

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

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.

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

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.

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);
}

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

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?

解决方案

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天全站免登陆