• WPF全局逻辑未处理异常的异常处理


    public partial class App : Application
    {
        /// <summary>
        ///  异常处理
        /// </summary>
        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);
            //捕获程序中未处理的异常(UI线程)
            Application.Current.DispatcherUnhandledException += App_DispatcherUnhandledException;
            //Task线程内未捕获异常处理事件
            TaskScheduler.UnobservedTaskException += TaskScheduler_UnobservedTaskException;
            // 捕获程序中未处理的异常(非UI线程异常)
            AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
        }
        /// <summary>
        /// 捕获程序中未处理的异常(UI线程)
        /// </summary>
        private void App_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
        {
            Exception ex = e.Exception;
            if (ex.Message.Contains("因此无法排序数据集合"))
            {
                MessageBox.Show("您对\"序号\"等非数据列进行了无意义的排序!");
                e.Handled = true;
                return;
            };
    
            string str = ex.StackTrace;
            //Log4NetHelper.WriteError(typeof(Exception), "UI线程异常;异常位置:" + str.Substring(str.LastIndexOf("\\") + 1, str.Length - str.LastIndexOf("\\") - 1) + ";异常信息:" + ex.Message);
            e.Handled = true;
            MessageBox.Show("软件异常报错:"+ ex.Message,"软件未处理异常");
        }
    
        /// <summary>
        /// Task线程内未捕获异常处理事件
        /// </summary>
        private void TaskScheduler_UnobservedTaskException(object? sender, UnobservedTaskExceptionEventArgs e)
        {
            Exception ex = e.Exception;
            string str = ex.StackTrace;
            //Log4NetHelper.WriteError(typeof(Exception), "Task线程里异常;异常位置:" + str.Substring(str.LastIndexOf("\\") + 1, str.Length - str.LastIndexOf("\\") - 1) + ";异常信息:" + ex.Message);
        }
        /// <summary>
        /// 捕获程序中未处理的异常(非UI线程异常)
        /// </summary>
        private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            Exception ex = e.ExceptionObject as Exception;
            string str = ex.StackTrace;
            //Log4NetHelper.WriteError(typeof(Exception), "非UI线程异常;异常位置:" + str.Substring(str.LastIndexOf("\\") + 1, str.Length - str.LastIndexOf("\\") - 1) + ";异常信息:" + ex.Message);
            MessageBox.Show("软件致命报错:" + ex.Message, "软件致命异常");
        }
    }
  • 相关阅读:
    大数据-KNN算法
    Java-单链表的实现
    Python分析《武林外传》
    Java-javaFx库运用-自动弹跳的球
    Java-javaFx库运用-时钟显示
    Python爬虫-《神雕侠侣》
    大数据-决策树
    Java--用键盘控制小球
    Java--下大雪模拟
    Java File ——Io流复习
  • 原文地址:https://www.cnblogs.com/qq2806933146xiaobai/p/16832285.html
Copyright © 2020-2023  润新知