我正在为一个全局事件管理器类使用单例模式,该类不以可接受的方式处理异常。
如果正在执行的某个事件调用的代码中抛出异常,我总是会收到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块没有捕获异常。
答案 0 :(得分:1)
您所描述的TargetInvocationException
几乎总是会有InnerException
的原始例外。