C# WinForm捕獲全局變量異常 SamWang解決方法
更新時間:2012年11月14日 11:43:07 作者:
本文將介紹C# WinForm捕獲全局變量異常 SamWang解決方法,需要的朋友可以參考
許多小公司的項目都缺少異常處理模塊,我們也是。經(jīng)常會出現(xiàn)這種情況,用戶在UI界面操作,就直接跳出堆棧調(diào)用的異常信息對話框,老板看到那叫一個火??!你們的代碼怎么天天出現(xiàn)亂碼。呵呵!這就是沒有異常捕獲處理導(dǎo)致的,現(xiàn)在許多人寫代碼都沒意識處理異常,只要實現(xiàn)功能就好,我的許多組員也是如此。
項目剛接手,所以打算做一個異常全局捕獲,統(tǒng)一處理的模式,采用具體詳細(xì)信息的對話框提醒與日志文件保存方式。以下是根據(jù)網(wǎng)上找的C#winform全局異常捕獲做了點修改。(等項目異常處理全部完成后,將心得體會做個記錄,此處暫對全局異常捕獲做個記錄)
static class Program
{
/// <summary>
/// 應(yīng)用程序的主入口點。
/// </summary>
[STAThread]
static void Main()
{
try
{
//設(shè)置應(yīng)用程序處理異常方式:ThreadException處理
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
//處理UI線程異常
Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
//處理非UI線程異常
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
#region 應(yīng)用程序的主入口點
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
#endregion
}
catch (Exception ex)
{
string str = GetExceptionMsg(ex,string.Empty);
MessageBox.Show(str, "系統(tǒng)錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
{
string str = GetExceptionMsg(e.Exception, e.ToString());
MessageBox.Show(str, "系統(tǒng)錯誤", 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, "系統(tǒng)錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error);
//LogManager.WriteLog(str);
}
/// <summary>
/// 生成自定義異常消息
/// </summary>
/// <param name="ex">異常對象</param>
/// <param name="backStr">備用異常消息:當(dāng)ex為null時有效</param>
/// <returns>異常字符串文本</returns>
static string GetExceptionMsg(Exception ex,string backStr)
{
StringBuilder sb = new StringBuilder();
sb.AppendLine("****************************異常文本****************************");
sb.AppendLine("【出現(xiàn)時間】:" + DateTime.Now.ToString());
if (ex != null)
{
sb.AppendLine("【異常類型】:" + ex.GetType().Name);
sb.AppendLine("【異常信息】:" + ex.Message);
sb.AppendLine("【堆棧調(diào)用】:" + ex.StackTrace);
}
else
{
sb.AppendLine("【未處理異?!浚? + backStr);
}
sb.AppendLine("***************************************************************");
return sb.ToString();
}
}
參考:
static class Program
{
/// <summary>
/// 應(yīng)用程序的主入口點。
/// </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 應(yīng)用程序的主入口點
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Main());
#endregion
}
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)用程序線程錯誤:{0}", ex);
}
//MessageBox.Show(str, "系統(tǒng)錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error);
LogManager.WriteLog(str);
}
}
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)用程序線程錯誤:{0}", e);
}
//MessageBox.Show(str, "系統(tǒng)錯誤", 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 = "出現(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);
}
//MessageBox.Show(str, "系統(tǒng)錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error);
LogManager.WriteLog(str);
}
}
項目剛接手,所以打算做一個異常全局捕獲,統(tǒng)一處理的模式,采用具體詳細(xì)信息的對話框提醒與日志文件保存方式。以下是根據(jù)網(wǎng)上找的C#winform全局異常捕獲做了點修改。(等項目異常處理全部完成后,將心得體會做個記錄,此處暫對全局異常捕獲做個記錄)
復(fù)制代碼 代碼如下:
static class Program
{
/// <summary>
/// 應(yīng)用程序的主入口點。
/// </summary>
[STAThread]
static void Main()
{
try
{
//設(shè)置應(yīng)用程序處理異常方式:ThreadException處理
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
//處理UI線程異常
Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
//處理非UI線程異常
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
#region 應(yīng)用程序的主入口點
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
#endregion
}
catch (Exception ex)
{
string str = GetExceptionMsg(ex,string.Empty);
MessageBox.Show(str, "系統(tǒng)錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
{
string str = GetExceptionMsg(e.Exception, e.ToString());
MessageBox.Show(str, "系統(tǒng)錯誤", 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, "系統(tǒng)錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error);
//LogManager.WriteLog(str);
}
/// <summary>
/// 生成自定義異常消息
/// </summary>
/// <param name="ex">異常對象</param>
/// <param name="backStr">備用異常消息:當(dāng)ex為null時有效</param>
/// <returns>異常字符串文本</returns>
static string GetExceptionMsg(Exception ex,string backStr)
{
StringBuilder sb = new StringBuilder();
sb.AppendLine("****************************異常文本****************************");
sb.AppendLine("【出現(xiàn)時間】:" + DateTime.Now.ToString());
if (ex != null)
{
sb.AppendLine("【異常類型】:" + ex.GetType().Name);
sb.AppendLine("【異常信息】:" + ex.Message);
sb.AppendLine("【堆棧調(diào)用】:" + ex.StackTrace);
}
else
{
sb.AppendLine("【未處理異?!浚? + backStr);
}
sb.AppendLine("***************************************************************");
return sb.ToString();
}
}
參考:
復(fù)制代碼 代碼如下:
static class Program
{
/// <summary>
/// 應(yīng)用程序的主入口點。
/// </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 應(yīng)用程序的主入口點
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Main());
#endregion
}
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)用程序線程錯誤:{0}", ex);
}
//MessageBox.Show(str, "系統(tǒng)錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error);
LogManager.WriteLog(str);
}
}
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)用程序線程錯誤:{0}", e);
}
//MessageBox.Show(str, "系統(tǒng)錯誤", 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 = "出現(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);
}
//MessageBox.Show(str, "系統(tǒng)錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error);
LogManager.WriteLog(str);
}
}
相關(guān)文章
C#/VB.NET實現(xiàn)將XML轉(zhuǎn)為PDF
可擴(kuò)展標(biāo)記語言(XML)文件是一種標(biāo)準(zhǔn)的文本文件,它使用特定的標(biāo)記來描述文檔的結(jié)構(gòu)以及其他特性。本文將利用C#實現(xiàn)XML文件轉(zhuǎn)PDF?,需要的可以參考一下2022-03-03
.Net WInform開發(fā)筆記(二)Winform程序運(yùn)行結(jié)構(gòu)圖及TCP協(xié)議在Winform中的應(yīng)用
中午沒事,把去年剛畢業(yè)那會畫的幾張圖翻出來了,大概介紹Winform應(yīng)用程序運(yùn)行的過程,以及TCP協(xié)議在Winform中的應(yīng)用。感興趣的朋友可以了解下;如果有Windows消息機(jī)制等基礎(chǔ),很好理解這兩張2013-01-01

