美文网首页
WPF处理未捕获异常

WPF处理未捕获异常

作者: 奔跑伯爵 | 来源:发表于2020-08-16 21:25 被阅读0次

如果程序中出现未捕获的异常,程序通常会直接崩溃,也不知道哪里出了问题。在App.xaml.cs文件中加入以下代码,记录未捕获的异常,便于查找错误。

    public partial class App : Application
    {
        public App()
        {            
            this.Startup += (sender, e) =>
            {
                this.DispatcherUnhandledException += App_DispatcherUnhandledException;
                TaskScheduler.UnobservedTaskException += TaskScheduler_UnobservedTaskException;
                AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
            };
        }

        private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            try
            {
                if (e.ExceptionObject is Exception)
                    LogException("CurrentDomain_UnhandledException", e.ExceptionObject as Exception);
                else
                    MessageBox.Show(e.ExceptionObject.ToString(), "CurrentDomain_UnhandledException");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ExceptionToString(ex), "CurrentDomain_UnhandledException");
            }
        }

        private void TaskScheduler_UnobservedTaskException(object sender, UnobservedTaskExceptionEventArgs e)
        {
            try
            {
                LogException("TaskScheduler_UnobservedTaskException", e.Exception);
                e.SetObserved();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ExceptionToString(ex), "TaskScheduler_UnobservedTaskException");
            }
        }

        private void App_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
        {
            try
            {
                LogException("App_DispatcherUnhandledException", e.Exception);
                if (!(e.Exception is OutOfMemoryException))
                    e.Handled = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ExceptionToString(ex), "App_DispatcherUnhandledException");
            }
        }

        void ExToStr(StringBuilder sb, Exception ex)
        {
            sb.AppendLine(ex.GetType().ToString());
            sb.AppendLine(ex.Message);
            sb.AppendLine(ex.StackTrace);
            if (ex.InnerException != null)
            {
                sb.AppendLine("InnerException");
                ExToStr(sb, ex.InnerException);
            }
        }

        string ExceptionToString(Exception ex)
        {
            var sb = new StringBuilder();
            sb.AppendLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"));
            ExToStr(sb, ex);
            return sb.ToString();
        }

        void LogException(string caption, Exception ex)
        {
            string msg = ExceptionToString(ex);
            using (var sw = new StreamWriter(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "error.log"), true))
            {
                sw.WriteLine(caption);
                sw.WriteLine(msg);
            }
            MessageBox.Show(msg, caption);
        }
    }

相关文章

  • WPF处理未捕获异常

    如果程序中出现未捕获的异常,程序通常会直接崩溃,也不知道哪里出了问题。在App.xaml.cs文件中加入以下代码,...

  • WPF捕获全局未处理异常

    WPF中捕获全局异常并记录 应用有时候会异常崩溃,这时候如果有错误的堆栈信息,就很方便我们查找问题。捕获未处理异常...

  • 6未捕获的异常

    如果一个异常未被捕获,则由名为未捕获异常处理程序的函数截取。未捕获的异常处理程序总是导致程序退出,但可能会在这之前...

  • springboot 异常捕获和处理

    springboot 异常捕获和处理 异常捕获处理

  • 未捕获异常的处理

    当一个线程由于未捕获异常而退出时,JVM会把这个事件报告给应用程序提供的UncaughtExceptionHand...

  • SpringMVC处理未捕获异常

  • Java未捕获异常处理

    https://blog.csdn.net/qq_36186690/article/details/82940032

  • Python异常处理

    Python异常处理 一、捕获异常 1.异常捕获语法 2.错误类型捕获 except:处理所有异常。except ...

  • WPF全局异常捕获

    跟着《WPF专业编程开发指南》这书打的代码的,自己在正式项目中测试通过,可以抓取到全局的异常,用的log4net来...

  • CrashHandler未捕获类型异常处理

    当用户在使用app出现崩溃现象时,我们需要知道是什么原因,并将原因记录下来上到服务器,这样以后我们就可以知道具体是...

网友评论

      本文标题:WPF处理未捕获异常

      本文链接:https://www.haomeiwen.com/subject/wsmbhftx.html