zoukankan      html  css  js  c++  java
  • [WPF 学习] 4.未处理异常的处理

    异常不可避免,所有地方都写try...catch也麻烦,所以有了未处理异常的处理的东东,分别为以下三个事件:

    1. Application.Current.DispatcherUnhandledException:DispatcherUnhandledException is raised by an Application for each exception that is unhandled by code running on the main UI thread.(UI线程中任何未处理的异常将触发该事件)
    2. System.Threading.Tasks.TaskScheduler.UnobservedTaskException:Occurs when a faulted task's unobserved exception is about to trigger exception escalation policy(出错的任务中未观察到的异常将触发该事件)。在Task被垃圾回收的时候,析构函数检测到该Task对象还有未被处理过的异常,会抛出这个异常并触发,所以感觉总是慢一拍。
    3. AppDomain.CurrentDomain.UnhandledException:应用程序域的未处理异常

    第1、2个事件很好理解,在UI线程和任务线程throw new Excepiton("测试")都能测试出来,第3个事件我是折腾好久才找到触发的地方——比如在1、2事件的方法中又发生了异常,所以也可以理解1、2事件都有参数和方法可以设置成已处理(e.Handled=True、e.SetObserved()),第3个事件一被触发,game over。

    最后附上我的代码

    using System;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows;
    
    namespace L3_Exception
    {
        /// <summary>
        /// Interaction logic for App.xaml
        /// </summary>
        public partial class App : Application
        {
            public App()
            {
                Application.Current.DispatcherUnhandledException += Current_DispatcherUnhandledException;
                AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
                TaskScheduler.UnobservedTaskException += TaskScheduler_UnobservedTaskException;
            }
           
            /// <summary>
            /// 非主线程错误
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private static void TaskScheduler_UnobservedTaskException(object sender, UnobservedTaskExceptionEventArgs e)
            {
                StringBuilder sb = new StringBuilder();
                foreach (Exception item in e.Exception.InnerExceptions)
                {
                    sb.AppendLine($@"异常类型:{item.GetType()}
    异常内容:{item.Message}
    来自:{item.Source}
    {item.StackTrace}");
                }
                e.SetObserved();
                Restart("Task Exception", sb.ToString());
            }
            /// <summary>
            /// App里面的错误
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
            {
                StringBuilder sb = new StringBuilder();
                try
                {
                    Exception ex = e.ExceptionObject as Exception;
                    sb.AppendLine($@"异常类型:{ ex.GetType()}
    异常内容:{ ex.Message}
    内部异常内容:{ex?.InnerException?.Message}
    来自:{ ex.Source}
    { ex.StackTrace}");
    
                }
                catch
                {
                    sb.AppendLine("不可恢复的WPF窗体线程异常");
                }
    
                Restart("Domain Exception", sb.ToString());
            }
            /// <summary>
            /// 主线程错误
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private static void Current_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
            {
                StringBuilder sb = new StringBuilder();
    
                sb.AppendLine($@"异常类型:{ e.Exception.GetType()}
    异常内容:{ e.Exception.Message}
    内部异常内容:{e.Exception?.InnerException?.Message}
    来自:{ e.Exception.Source}
    { e.Exception.StackTrace}");
                e.Handled = true;
                Restart("主线程异常", sb.ToString());
            }
    
    
            private static void Restart(string title, string content)
            {
                MessageBox.Show(content, title);
                //Current.Dispatcher.Invoke(() => Current.Shutdown(-1));
            }
        }
    }
    
    
  • 相关阅读:
    NeoFinder for Mac(增强型文件管理工具)破解版安装
    Monodraw for Mac(基于 ASCII 码设计编辑工具)破解版安装
    SNF开发平台WinForm-审核流使用方法样例
    SNF快速开发平台MVC-Grid++集成打印
    SNF快速开发平台MVC-富文本控件集成了百度开源项目editor
    C#按回车Enter使输入焦点自动跳到下一个TextBox的方法收集
    SNF快速开发平台MVC-EasyUI3.9之-WebApi和MVC-controller层接收的json字符串的取值方法和调用后台服务方法
    SNF快速开发平台--规则引擎在程序当中如何调用
    SNF快速开发平台--规则引擎介绍和使用文档
    SNF快速开发平台MVC-EasyUI3.9之-DataGrid表格控件如何增加右键菜单
  • 原文地址:https://www.cnblogs.com/moon3/p/12710243.html
Copyright © 2011-2022 走看看