C# WinForm捕獲全域變數異常 SamWang解決方案

來源:互聯網
上載者:User

許多小公司的項目都缺少異常處理模組,我們也是。經常會出現這種情況,使用者在UI介面操作,就直接跳出堆棧調用的異常資訊對話方塊,老闆看到那叫一個火啊!你們的代碼怎麼天天出現亂碼。呵呵!這就是沒有異常捕獲處理導致的,現在許多人寫代碼都沒意識處理異常,只要實現功能就好,我的許多組員也是如此。

項目剛接手,所以打算做一個異常全域捕獲,統一處理的模式,採用具體詳細資料的對話方塊提醒與記錄檔儲存方式。以下是根據網上找的C#winform全域異常捕獲做了點修改。(等項目異常處理全部完成後,將心得體會做個記錄,此處暫對全域異常捕獲做個記錄)   複製代碼 代碼如下:static class Program
{
/// <summary>
/// 應用程式的主進入點。
/// </summary>
[STAThread]
static void Main()
{
try
{
//設定應用程式處理異常方式:ThreadException處理
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
//處理UI線程異常
Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
//處理非UI線程異常
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);

#region 應用程式的主進入點
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
#endregion
}
catch (Exception ex)
{
string str = GetExceptionMsg(ex,string.Empty);
MessageBox.Show(str, "系統錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}

static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
{
string str = GetExceptionMsg(e.Exception, e.ToString());
MessageBox.Show(str, "系統錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error);
//LogManager.WriteLog(str);
}

static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
string str = GetExceptionMsg(e.ExceptionObject as Exception, e.ToString());
MessageBox.Show(str, "系統錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error);
//LogManager.WriteLog(str);
}

/// <summary>
/// 產生自訂異常訊息
/// </summary>
/// <param name="ex">異常對象</param>
/// <param name="backStr">備用異常訊息:當ex為null時有效</param>
/// <returns>例外狀況字串文本</returns>
static string GetExceptionMsg(Exception ex,string backStr)
{
StringBuilder sb = new StringBuilder();
sb.AppendLine("****************************異常文本****************************");
sb.AppendLine("【出現時間】:" + DateTime.Now.ToString());
if (ex != null)
{
sb.AppendLine("【異常類型】:" + ex.GetType().Name);
sb.AppendLine("【異常資訊】:" + ex.Message);
sb.AppendLine("【堆棧調用】:" + ex.StackTrace);
}
else
{
sb.AppendLine("【未處理異常】:" + backStr);
}
sb.AppendLine("***************************************************************");
return sb.ToString();
}
}

參考: 複製代碼 代碼如下: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);

#region 應用程式的主進入點

Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Main());

#endregion

}
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);
}

//MessageBox.Show(str, "系統錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error);
LogManager.WriteLog(str);
}

}

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);
}

//MessageBox.Show(str, "系統錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error);
LogManager.WriteLog(str);
}

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);
}

//MessageBox.Show(str, "系統錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error);
LogManager.WriteLog(str);
}
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.