wpf程序异常捕获,主线程捕获和线程捕获,全局性方法

来源:互联网 发布:mac wifi 未安装硬件 编辑:程序博客网 时间:2024/06/06 12:00


1.这个捕获还是会退出的,主要是找到异常所在

AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);

void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            try
            {
                Exception ex = e.ExceptionObject as Exception;
                string errorMsg = "非WPF窗体线程异常 : \n\n";
                ImLog.Log(errorMsg + ex.Message + Environment.NewLine + ex.StackTrace);
            }
            catch
            {
                MessageBox.Show("不可恢复的WPF窗体线程异常,应用程序将退出!");
            }
        }

2.捕获主线程所有未被捕获的异常

private void App_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
        {
            System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
            stringBuilder.AppendFormat("应用程序出现了未捕获的异常,{0}/n", e.Exception.Message);
            if (e.Exception.InnerException != null)
            {
                stringBuilder.AppendFormat("/n {0}", e.Exception.InnerException.Message);
            }
            stringBuilder.AppendFormat("/n {0}", e.Exception.StackTrace);
            ImLog.Log(stringBuilder.ToString());
            e.Handled = true;

        }

0 0
原创粉丝点击