WPF 捕捉全局异常

2022/2/21 23:26:41

本文主要是介绍WPF 捕捉全局异常,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

 

参考:https://www.cnblogs.com/snow-zhang/p/10107108.html

写在App.xaml.cs中

 void App_OnStartup(object sender, StartupEventArgs e)
        {
            //UI线程未捕获异常处理事件
            this.DispatcherUnhandledException += new DispatcherUnhandledExceptionEventHandler(App_DispatcherUnhandledException);
            //Task线程内未捕获异常处理事件
            TaskScheduler.UnobservedTaskException += TaskScheduler_UnobservedTaskException;
           // 非UI线程未捕获异常处理事件
            AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
        }

        void App_Exit(object sender, ExitEventArgs e)
        {
           // 程序退出时需要处理的业务
        }

        void App_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
        {
            try
            {
                e.Handled = true; //把 Handled 属性设为true,表示此异常已处理,程序可以继续运行,不会强制退出      
                MessageBox.Show("UI线程异常:" + e.Exception.Message);
            }
            catch (Exception ex)
            {
                //此时程序出现严重异常,将强制结束退出
                MessageBox.Show("UI线程发生致命错误!");
            }

        }

        void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            StringBuilder sbEx = new StringBuilder();
            if (e.IsTerminating)
            {
                sbEx.Append("非UI线程发生致命错误");
            }
            sbEx.Append("非UI线程异常:");
            if (e.ExceptionObject is Exception)
            {
                sbEx.Append(((Exception)e.ExceptionObject).Message);
            }
            else
            {
                sbEx.Append(e.ExceptionObject);
            }
            MessageBox.Show(sbEx.ToString());
        }

        void TaskScheduler_UnobservedTaskException(object sender, UnobservedTaskExceptionEventArgs e)
        {
            //task线程内未处理捕获
            MessageBox.Show("Task线程异常:" + e.Exception.Message);
            e.SetObserved();//设置该异常已察觉(这样处理后就不会引起程序崩溃)
        }

 

完善一下

 private void App_OnStartup(object sender, StartupEventArgs e)
        {
            InitExceptionHandle();
        }
        public static void Close(int exitCode = 0)
        {
            Environment.Exit(exitCode);
        }
        private static void OnUnhandledException(Exception e)
        {
            App.Current.Dispatcher.Invoke(() =>
            {
                lock (App.Current)
                {
                    System.Console.WriteLine("Exception #{0}", e);
                    //  App.Close();

                }
            });
        }
        private void InitExceptionHandle()
        {
            this.DispatcherUnhandledException += (o, args) =>
            {
                args.Handled = true;
                OnUnhandledException(args.Exception);
            };
            TaskScheduler.UnobservedTaskException += (o, args) =>
            {
                OnUnhandledException(args.Exception);
            };
            AppDomain.CurrentDomain.UnhandledException += (o, args) =>
            {
                if (args.ExceptionObject is Exception e)
                {
                    OnUnhandledException(e);
                }
                else
                {
                    System.Console.WriteLine("Exception #{0}", args.ExceptionObject);
                    
                }
            };
        }

 



这篇关于WPF 捕捉全局异常的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程