国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > C# > 正文

C# WinForm捕獲未處理的異常實(shí)例解析

2020-01-24 02:26:08
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

本文以一個(gè)完整的實(shí)例形式講述了C# WinForm捕獲未處理的異常的方法。分享給大家供大家參考之用。具體代碼如下:

using System;using System.Collections.Generic;using System.Windows.Forms;using System.IO;namespace GobalException{  static class Program  {    /// <summary>    /// 應(yīng)用程序的主入口點(diǎn)。    /// </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)應(yīng)用程序未處理的異常:" + 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("應(yīng)用程序線程錯(cuò)誤:{0}", ex);        }        writeLog(str);//frmBug f = new frmBug(str);//友好提示界面      //f.ShowDialog();        MessageBox.Show("發(fā)生致命錯(cuò)誤,請(qǐng)及時(shí)聯(lián)系作者!", "系統(tǒng)錯(cuò)誤", MessageBoxButtons.OK, MessageBoxIcon.Error);      }    }    /// <summary>    ///這就是我們要在發(fā)生未處理異常時(shí)處理的方法,我這是寫(xiě)出錯(cuò)詳細(xì)信息到文本,如出錯(cuò)后彈出一個(gè)漂亮的出錯(cuò)提示窗體,給大家做個(gè)參考    ///做法很多,可以是把出錯(cuò)詳細(xì)信息記錄到文本、數(shù)據(jù)庫(kù),發(fā)送出錯(cuò)郵件到作者信箱或出錯(cuò)后重新初始化等等    ///這就是仁者見(jiàn)仁智者見(jiàn)智,大家自己做了。    /// </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)應(yīng)用程序未處理的異常:" + 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("應(yīng)用程序線程錯(cuò)誤:{0}", e);      }      writeLog(str);  //frmBug f = new frmBug(str);//友好提示界面      //f.ShowDialog();      MessageBox.Show("發(fā)生致命錯(cuò)誤,請(qǐng)及時(shí)聯(lián)系作者!", "系統(tǒng)錯(cuò)誤", MessageBoxButtons.OK, MessageBoxIcon.Error);    }    static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)    {      string str = "";      Exception error = e.ExceptionObject as Exception;      string strDateInfo = "出現(xiàn)應(yīng)用程序未處理的異常:" + 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ā)生致命錯(cuò)誤,請(qǐng)停止當(dāng)前操作并及時(shí)聯(lián)系作者!", "系統(tǒng)錯(cuò)誤", MessageBoxButtons.OK, MessageBoxIcon.Error);    }    /// <summary>    /// 寫(xiě)文件    /// </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();      }    }  }}

本文實(shí)例配有較為詳盡的注釋,便于大家閱讀理解。希望本文所述對(duì)大家的C#程序設(shè)計(jì)有所幫助。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 绵竹市| 桦川县| 三江| 奉贤区| 麟游县| 阜新市| 湘潭县| 蓬溪县| 临泽县| 石河子市| 江源县| 筠连县| 黑龙江省| 太康县| 旬阳县| 错那县| 闽侯县| 两当县| 如皋市| 临潭县| 麻栗坡县| 安塞县| 松桃| 吴江市| 贺兰县| 泸水县| 宁海县| 西林县| 阳谷县| 阿城市| 大石桥市| 泰来县| 浙江省| 阿合奇县| 珲春市| 名山县| 汉中市| 仙桃市| 调兵山市| 珠海市| 夏津县|