C# WinForm捕獲未處理的異常實例解析
更新時間:2014年09月10日 11:37:10 投稿:shichen2014
這篇文章主要介紹了C# WinForm捕獲未處理的異常,包括了常見的未捕獲的異常、UI線程異常、非UI線程異常等,非常實用,需要的朋友可以參考下
本文以一個完整的實例形式講述了C# WinForm捕獲未處理的異常的方法。分享給大家供大家參考之用。具體代碼如下:
using System; using System.Collections.Generic; using System.Windows.Forms; using System.IO; namespace GobalException { static class Program { /// <summary> /// 應用程序的主入口點。 /// </summary> [STAThread] static void Main() { try { //處理未捕獲的異常 Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException); //處理UI線程異常 Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException); //處理非UI線程異常 AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException); Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); } catch (Exception ex) { string str = ""; string strDateInfo = "出現(xiàn)應用程序未處理的異常:" + 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); } writeLog(str); //frmBug f = new frmBug(str);//友好提示界面 //f.ShowDialog(); MessageBox.Show("發(fā)生致命錯誤,請及時聯(lián)系作者!", "系統(tǒng)錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error); } } /// <summary> ///這就是我們要在發(fā)生未處理異常時處理的方法,我這是寫出錯詳細信息到文本,如出錯后彈出一個漂亮的出錯提示窗體,給大家做個參考 ///做法很多,可以是把出錯詳細信息記錄到文本、數(shù)據(jù)庫,發(fā)送出錯郵件到作者信箱或出錯后重新初始化等等 ///這就是仁者見仁智者見智,大家自己做了。 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e) { string str = ""; string strDateInfo = "出現(xiàn)應用程序未處理的異常:" + 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); } writeLog(str); //frmBug f = new frmBug(str);//友好提示界面 //f.ShowDialog(); MessageBox.Show("發(fā)生致命錯誤,請及時聯(lián)系作者!", "系統(tǒng)錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error); } static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) { string str = ""; Exception error = e.ExceptionObject as Exception; string strDateInfo = "出現(xiàn)應用程序未處理的異常:" + 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); } writeLog(str); //frmBug f = new frmBug(str);//友好提示界面 //f.ShowDialog(); MessageBox.Show("發(fā)生致命錯誤,請停止當前操作并及時聯(lián)系作者!", "系統(tǒng)錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error); } /// <summary> /// 寫文件 /// </summary> /// <param name="str"></param> static void writeLog(string str) { if (!Directory.Exists("ErrLog")) { Directory.CreateDirectory("ErrLog"); } using (StreamWriter sw = new StreamWriter(@"ErrLog\ErrLog.txt", true)) { sw.WriteLine(str); sw.WriteLine("---------------------------------------------------------"); sw.Close(); } } } }
本文實例配有較為詳盡的注釋,便于大家閱讀理解。希望本文所述對大家的C#程序設計有所幫助。
相關文章
C#動態(tài)創(chuàng)建button按鈕的方法實例詳解
這篇文章主要介紹了C#動態(tài)創(chuàng)建button按鈕的方法實例詳解的相關資料,需要的朋友可以參考下2017-06-06C#(.Net)將非托管dll嵌入exe中的實現(xiàn)
本文主要介紹了C#(.Net)將非托管dll嵌入exe中的實現(xiàn),文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-12-12C#實現(xiàn)延時并自動關閉MessageBox的方法
這篇文章主要介紹了C#實現(xiàn)延時并自動關閉MessageBox的方法,非常實用的功能,需要的朋友可以參考下2014-08-08