我們在做WinForm程序的時候,一般都是對異常進行處理,但是,我們要防止不小心出現未知異常,導致軟體崩潰。也可採集系統未知的異常信息,防止出現異常,也無法下手。於是就有了如這篇文章標題所述的一個簡單的需求。
代碼實現1、處理未捕獲的異常
static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e) {
string str = ""; string strDateInfo = "出現應用程式未處理的異常:" + DateTime.Now.ToString() + "\r\n"; Exception error = e.Exception as Exception; if (error != null) { str = string.Format(strDateInfo + "異常類型:{0}\r\n異常消息:{1}\r\n異常信息:{2}\r\n", error.GetType().Name, error.Message, error.StackTrace); } else { str = string.Format("應用程式線程錯誤:{0}", e); } Helper.GetInstance().PlanLog(str, LogType.應用程式異常.ToString()); MessageBox.Show("發生致命錯誤,請及時聯繫作者!", "系統錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error); }2、處理UI線程異常
static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) { string str = ""; Exception error = e.ExceptionObject as Exception; string strDateInfo = "出現應用程式未處理的異常:" + DateTime.Now.ToString() + "\r\n"; if (error != null) { str = string.Format(strDateInfo + "Application UnhandledException:{0};\n\r堆棧信息:{1}", error.Message, error.StackTrace); } else { str = string.Format("Application UnhandledError:{0}", e); } Helper.GetInstance().PlanLog(str, LogType.應用程式異常.ToString()); MessageBox.Show("發生致命錯誤,請停止當前操作並及時聯繫作者!", "系統錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error); }3、處理非UI線程異常
[STAThread] static void Main() { try { Thread _UserMessageThread; _UserMessageThread = new Thread(new ThreadStart(LoginManager.GetInstance().test)); _UserMessageThread.IsBackground = true; _UserMessageThread.Start();
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException); Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException); AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException); Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Xw.Common.Sys.SysConfig.AppExwcutePath = Application.ExecutablePath; Xw.Common.Sys.SysConfig.AppStartPath = Application.StartupPath; Xw.Common.Sys.SysConfig.Version = "V1.0.0"; Xw.Common.Sys.SysConfig.SoftFullName = "拍鞋網"; Xw.Common.Sys.SysConfig.SoftName = "軟體園店"; if (!Xw.Common.Sys.SysConfig.IsOnlyRunSoft("PaiXie.Pos.Client")) { Xw.Common.Sys.MsgBoxWin.ShowInformation("該程序已運行!"); return; } Application.Run(new Login()); } catch (Exception ex) { string str = ""; string strDateInfo = "出現應用程式未處理的異常:" + DateTime.Now.ToString() + "\r\n"; if (ex != null) { str = string.Format(strDateInfo + "異常類型:{0}\r\n異常消息:{1}\r\n異常信息:{2}\r\n", ex.GetType().Name, ex.Message, ex.StackTrace); } else { str = string.Format("應用程式線程錯誤:{0}", ex); } Helper.GetInstance().PlanLog(str, LogType.應用程式異常.ToString()); MessageBox.Show("發生致命錯誤,請及時聯繫作者!", "系統錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error); } }完整代碼using PaiXie.Pos.Client.Core;using PaiXie.Utils;using System;using System.Collections.Generic;using System.Linq;using System.Threading;using System.Windows.Forms;namespace PaiXie.Pos.Client { static class Program { [STAThread] static void Main() { try { Thread _UserMessageThread; _UserMessageThread = new Thread(new ThreadStart(LoginManager.GetInstance().test)); _UserMessageThread.IsBackground = true; _UserMessageThread.Start();
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException); Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException); AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException); Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Xw.Common.Sys.SysConfig.AppExwcutePath = Application.ExecutablePath; Xw.Common.Sys.SysConfig.AppStartPath = Application.StartupPath; Xw.Common.Sys.SysConfig.Version = "V1.0.0"; Xw.Common.Sys.SysConfig.SoftFullName = "拍鞋網"; Xw.Common.Sys.SysConfig.SoftName = "軟體園店"; if (!Xw.Common.Sys.SysConfig.IsOnlyRunSoft("PaiXie.Pos.Client")) { Xw.Common.Sys.MsgBoxWin.ShowInformation("該程序已運行!"); return; } Application.Run(new Login()); } catch (Exception ex) { string str = ""; string strDateInfo = "出現應用程式未處理的異常:" + DateTime.Now.ToString() + "\r\n"; if (ex != null) { str = string.Format(strDateInfo + "異常類型:{0}\r\n異常消息:{1}\r\n異常信息:{2}\r\n", ex.GetType().Name, ex.Message, ex.StackTrace); } else { str = string.Format("應用程式線程錯誤:{0}", ex); } Helper.GetInstance().PlanLog(str, LogType.應用程式異常.ToString()); MessageBox.Show("發生致命錯誤,請及時聯繫作者!", "系統錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error); } } static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e) {
string str = ""; string strDateInfo = "出現應用程式未處理的異常:" + DateTime.Now.ToString() + "\r\n"; Exception error = e.Exception as Exception; if (error != null) { str = string.Format(strDateInfo + "異常類型:{0}\r\n異常消息:{1}\r\n異常信息:{2}\r\n", error.GetType().Name, error.Message, error.StackTrace); } else { str = string.Format("應用程式線程錯誤:{0}", e); } Helper.GetInstance().PlanLog(str, LogType.應用程式異常.ToString()); MessageBox.Show("發生致命錯誤,請及時聯繫作者!", "系統錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error); } static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) { string str = ""; Exception error = e.ExceptionObject as Exception; string strDateInfo = "出現應用程式未處理的異常:" + DateTime.Now.ToString() + "\r\n"; if (error != null) { str = string.Format(strDateInfo + "Application UnhandledException:{0};\n\r堆棧信息:{1}", error.Message, error.StackTrace); } else { str = string.Format("Application UnhandledError:{0}", e); } Helper.GetInstance().PlanLog(str, LogType.應用程式異常.ToString()); MessageBox.Show("發生致命錯誤,請停止當前操作並及時聯繫作者!", "系統錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error); } }}總結針對異常,我們肯定無法事先全部預知,所以進行全局異常捕獲還是很有必要的。