WPF 全局异常处理

在Application中存在三种异常事件EventHandler

  • DispatcherUnhandledException
  • AppDomain.CurrentDomain.UnhandledException
  • TaskScheduler.UnobservedTaskException

其中 DispatcherUnhandledException 是在异常由应用程序引发但未进行处理时发生,但无法捕获多线程异常
AppDomain.CurrentDomain.UnhandledException专门捕获所有线程中的异常(不包括Task)
TaskScheduler.UnobservedTaskException 捕获Task中的异常

这些异常Handler可以在应用程序出现异常是记录日志,或者挽回应用程序奔溃的问题。

举例说明

以下异常会触发 DispatcherUnhandledException 以及 AppDomain.CurrentDomain.UnhandledException
执行顺序是 DispatcherUnhandledException => AppDomain.CurrentDomain.UnhandledException

int x = 0;
_ = 1 / x;

以下异常会触发 AppDomain.CurrentDomain.UnhandledException

new Thread(() => { _ = 1 / x; }).Start();

以下异常会触发 TaskScheduler.UnobservedTaskException
Task中的异常并不是立刻就能捕获到的,而是等到垃圾回收的时候进行捕获。如果想立刻进行捕获则可以调用GC.Collect(0);和GC.WaitForPendingFinalizers();

Task.Run(() => { _ = 1 / x; });

以下是在Prism框架下的异常处理,其中 Task异常不会导致应用程序奔溃,
DispatcherUnhandledException异常可以通过e.Handled = true;表明该异常已被处理,不会造成程序崩溃和退出。
AppDomain.CurrentDomain.UnhandledException 在.Net FrameWork中可以通过设置在 App.config <runtime> 节点下添加 <legacyUnhandledExceptionPolicy enabled="1"/> 可以阻止应用程序奔溃,但是这边我使用的是Net6.0所以没成功!

public partial class App
    {
        protected override Window CreateShell()
        {
            return Container.Resolve<MainWindow>();
        }

        protected override void OnInitialized()
        {
            #region 全局异常事件配置
            // 在异常由应用程序引发但未进行处理时发生,但无法捕获多线程异常
            // UI线程中的异常 UnhandledException 和  DispatcherUnhandledException 都会捕获 执行顺序是 DispatcherUnhandledException => UnhandledException
            this.DispatcherUnhandledException += App_DispatcherUnhandledException;
            // 专门捕获所有线程中的异常 
            AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
            // 专门捕获Task异常
            TaskScheduler.UnobservedTaskException += TaskScheduler_UnobservedTaskException;
            #endregion

            // 初始化menu 并导航到一个页面
            IStartService startService = App.Current.MainWindow.DataContext as IStartService;
            startService.Start();
            base.OnInitialized();
        }

        private void TaskScheduler_UnobservedTaskException(object sender, UnobservedTaskExceptionEventArgs e)
        {
            Console.WriteLine("---捕获Task异常---");
            e.SetObserved();
        }

        private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            Console.WriteLine("---所有线程中的异常---");
            MessageBox.Show((e.ExceptionObject as Exception).Message);
            //在.Net6.0中无效 --- 在 app.config <runtime> 节点下添加 <legacyUnhandledExceptionPolicy enabled="1"/> 可以阻止应用程序奔溃类似 e.Handle=true

        }

        private void App_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
        {
            MessageBox.Show("应用程序异常:" + e.Exception.Message);
            // 表明该异常已被处理,不会造成程序崩溃和退出
            e.Handled = true;
        }

        protected override void RegisterTypes(IContainerRegistry containerRegistry)
        {

        }

        protected override void ConfigureModuleCatalog(IModuleCatalog moduleCatalog)
        {
            // 添加Coding模块
            moduleCatalog.AddModule<CodingModuleModule>();
            base.ConfigureModuleCatalog(moduleCatalog);
        }

        
    }
  • 10
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值