wpf - UnhandledExceptionFilter 捕获所有异常但只重新抛出一些异常

标签 wpf exception add-in dispatcher unhandled-exception

我正在使用 WPF 窗口开发一个 Word 加载项。我正在使用 UnhandledExceptionFilter 来捕获任何未处理的异常,这样 Word 就不会抛出错误消息。

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

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

我的事件被正确触发,我可以显示相应的错误消息框。但是,对于某些异常(例如窗口代码中的空指针异常),异常仍然被抛给调用类。而其他异常(例如从窗口中使用的另一个帮助程序类抛出的 EndpointNotFoundException)在我的事件中被捕获并且没有重新抛出。

有什么想法吗?谢谢。

我问了一个关于如何捕获未处理的异常的初始问题,但现在我遇到了这个额外的问题。

Catch C# WPF unhandled exception in Word Add-in before Microsoft displays error message

最佳答案

我通过使用 3 种不同的异常处理程序来解决此问题,因为这取决于抛出异常的方式和位置,您需要分别捕获它。这也让您有机会根据您自己对异常是否致命的决定取消终止并继续程序执行。

using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Threading;
    
public class ExceptionHelper
{
    public static ExceptionHelper()
    {
        TaskScheduler.UnobservedTaskException += TaskSchedulerOnUnobservedTaskException;
        Application.Current.Dispatcher.UnhandledException += DispatcherOnUnhandledException;
        AppDomain.CurrentDomain.UnhandledException += CurrentDomainUnhandledException;
    }

    private static void TaskSchedulerOnUnobservedTaskException(object sender, UnobservedTaskExceptionEventArgs e)
    {
        e?.SetObserved();
        CurrentDomainUnhandledException(sender, new UnhandledExceptionEventArgs(e.Exception, false));
    }
    
    private static void DispatcherOnUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
    {
        e.Handled = true;
        CurrentDomainUnhandledException(sender, new UnhandledExceptionEventArgs(e.Exception, false));
    }
    
    public static void CurrentDomainUnhandledException(object sender, [CanBeNull] UnhandledExceptionEventArgs e)
    {
        //Program is hung, ensure we don't infinite loop on outofmemory exception by removing the handler
        var isTerminating = e?.IsTerminating == true;
    
        if (isTerminating)
        {
            TaskScheduler.UnobservedTaskException -= TaskSchedulerOnUnobservedTaskException;
            Application.Current.Dispatcher.UnhandledException -= DispatcherOnUnhandledException;
            AppDomain.CurrentDomain.UnhandledException -= CurrentDomainUnhandledException;
        }
    
        var ex = (Exception)e?.ExceptionObject;
        
        
        //Kill the app if we weren't already terminating and the program is in a corrupted state.

        if(!isTerminating)
            Process.GetCurrentProcess().Kill();
    
    }
}

关于wpf - UnhandledExceptionFilter 捕获所有异常但只重新抛出一些异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12308950/

相关文章:

c# - 使用 DocumentPaginator 打印时如何打印预览?

wpf - 用户控件绑定(bind)将值传递给属性

c# - 如何设置 Bing map 样式?

java - 异常处理中的查询

c# - Application.COMAddIns 下标超出范围

wpf - 将焦点移动到 DataGrid (WPF) 中新行的第一列

java - 文件处理的资源泄漏,try with resources VS try-catch-finally with file.close()

java - 添加到集合时的现有 Java 异常会超过固定大小时?

c# - 获取 Outlook AppointmentItem 主题行的最新值

visual-studio - Visual Studio 2017 : QT Add-in