Microsoft显示错误消息之前,在Word加载项中捕获C#WPF未处理的异常 [英] Catch C# WPF unhandled exception in Word Add-in before Microsoft displays error message

查看:138
本文介绍了Microsoft显示错误消息之前,在Word加载项中捕获C#WPF未处理的异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用C#和WPF开发Microsoft Word加载项。

I am developing a Microsoft Word Add-in using C# and WPF.

在我的一个窗口中,辅助类在事件中引发异常。我希望异常上升到窗口级别,以便我可以捕获它并向用户显示错误消息。因为它是在helper类的一个事件中,所以我不能只在try / catch块中用一个try / catch块来包围窗口代码中的方法调用。

In one of my windows, a helper class is throwing an exception in an event. I want the exception to bubble up to the window level so that I can catch it and display an error message to the user. Because it's in an event in the helper class, I can't just surround a method call in the window code with a try/catch block to catch it.

应用程序。当前返回null,所以我不能使用Application Dispatcher。

Application.Current returns null so I cannot use the Application Dispatcher.

我可以使用 Dispatcher.CurrentDispatcher.UnhandledException 并添加一个 DispatcherUnhandledExceptionEventHandler 有效,并且捕获了异常。但是,Microsoft显示

I can use Dispatcher.CurrentDispatcher.UnhandledException and add a DispatcherUnhandledExceptionEventHandler to it. This works and the exception is caught. However, Microsoft displays the


应用程序中发生未处理的异常

unhandled exception occurred in your application

错误消息之前被调用了我的事件处理程序。

error message before my event handler is called.

我试图用错误的方式解决此问题吗?一种抑制Microsoft未处理的异常错误消息的方法?

Am I trying to solve this problem the wrong way or is there a way to suppress Microsoft's unhandled exception error message?

推荐答案

在Microsoft Word捕获异常并引发异常之前,使用UnhandledExceptionFilter捕获异常。

Use UnhandledExceptionFilter to catch the exception before Microsoft Word catches the exception and throws the unhandled exception message.

Dispatcher.CurrentDispatcher.UnhandledExceptionFilter += 
  new DispatcherUnhandledExceptionFilterEventHandler(Dispatcher_UnhandledExceptionFilter);

void Dispatcher_UnhandledExceptionFilter(object sender, DispatcherUnhandledExceptionFilterEventArgs e)
{
  e.RequestCatch = false;
  // Display error message or close the window.
}

请确保将RequestCatch设置为false,以便Word不会处理该异常。

Make sure to set RequestCatch to false so that Word does not handle the exception.

这篇关于Microsoft显示错误消息之前,在Word加载项中捕获C#WPF未处理的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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