C# WinForm捕獲未處理的異常執行個體解析

來源:互聯網
上載者:User

標籤:ade   space   處理   trace   格式   name   object   get   tom   

本文以一個完整的執行個體形式講述了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 = "出現應用程式未處理的異常:" + 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("發生致命錯誤,請及時聯絡作者!", "系統錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error);      }    }    /// <summary>    ///這就是我們要在發生未處理異常時處理的方法,我這是寫出錯詳細資料到文本,如出錯後彈出一個漂亮的出錯提示表單,給大家做個參考    ///做法很多,可以是把出錯詳細資料記錄到文本、資料庫,發送出錯郵件到作者信箱或出錯後重新初始化等等    ///這就是仁者見仁智者見智,大家自己做了。    /// </summary>    /// <param name="sender"></param>    /// <param name="e"></param>    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);      }      writeLog(str);//frmBug f = new frmBug(str);//友好提示介面      //f.ShowDialog();      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);      }      writeLog(str);//frmBug f = new frmBug(str);//友好提示介面      //f.ShowDialog();      MessageBox.Show("發生致命錯誤,請停止當前操作並及時聯絡作者!", "系統錯誤", 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# WinForm捕獲未處理的異常執行個體解析

本文地址:  http://www.paobuke.com/develop/c-develop/pbk23535.html






相關內容C# 中SharpMap的簡單使用執行個體詳解C#操作SQLite資料庫協助類詳解C#中DataTable 轉換為 Json的方法匯總(三種方法)C#如何消除驗證碼圖片的鋸齒效果
淺談C# 序列化與還原序列化幾種格式的轉換C#簡單實現防止多個程式啟動並執行方法C#中Winfrom預設IME的設定方法C#常用的命名規則匯總

C# WinForm捕獲未處理的異常執行個體解析

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.