1

我正在为一个未以可接受的方式处理异常的全局事件管理器类使用单例模式。

如果在由正在执行的事件之一调用的代码中引发异常,我总是会收到Exception has been thrown by the target of an invocation.错误消息。此错误不包含与原始异常相关的信息,因此很难调试任何错误。

有没有办法将原始异常信息传回事件管理器?

public class ApplicationSettings
{
    private static EventManager _manager = new EventManager();

    public static EventManager EventManager
    {
        get { return _manager; }
    }
}

事件管理器类:

public class EventManager
{
    public event EventHandler<ReportExecutionArgs> ExecuteReportCurrentPage;
    public event EventHandler<ReportExecutionArgs> ExecuteReportNewPage;

    public virtual void OnExecuteReportCurrentPage(object sender, ReportExecutionArgs e)
    {
        try
        {
            if (this.ExecuteReportCurrentPage != null)
                this.ExecuteReportCurrentPage(sender, e);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
    }

    public virtual void OnExecuteReportNewPage(object sender, ReportExecutionArgs e)
    {
        try
        {
            if (this.ExecuteReportNewPage != null)
                this.ExecuteReportNewPage(sender, e);
        }
        catch(Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
    }
}

其他一些类将处理这些事件

ApplicationSettings.EventManager.ExecuteReportNewPage += new EventHandler<ReportExecutionArgs>(reportOpenedNewPage);

private void reportOpenedNewPage(object sender, ReportExecutionArgs e)
    {
        //something in this code throws an error
        LitePage page = new LitePage();
        _tabs.AddPage(page);
        Report report = setReport(page, e);
    }

编辑

澄清一下,OnExecuteReport方法中的 try/catch 块没有捕获异常。

4

1 回答 1

1

像您描述的那样的ATargetInvocationException几乎总是在它的InnerException.

于 2012-04-23T15:54:21.320 回答