C# WinForm捕獲全局變量異常 SamWang解決方法
更新時(shí)間:2012年11月14日 11:43:07 作者:
本文將介紹C# WinForm捕獲全局變量異常 SamWang解決方法,需要的朋友可以參考
許多小公司的項(xiàng)目都缺少異常處理模塊,我們也是。經(jīng)常會(huì)出現(xiàn)這種情況,用戶在UI界面操作,就直接跳出堆棧調(diào)用的異常信息對(duì)話框,老板看到那叫一個(gè)火啊!你們的代碼怎么天天出現(xiàn)亂碼。呵呵!這就是沒(méi)有異常捕獲處理導(dǎo)致的,現(xiàn)在許多人寫代碼都沒(méi)意識(shí)處理異常,只要實(shí)現(xiàn)功能就好,我的許多組員也是如此。
項(xiàng)目剛接手,所以打算做一個(gè)異常全局捕獲,統(tǒng)一處理的模式,采用具體詳細(xì)信息的對(duì)話框提醒與日志文件保存方式。以下是根據(jù)網(wǎng)上找的C#winform全局異常捕獲做了點(diǎn)修改。(等項(xiàng)目異常處理全部完成后,將心得體會(huì)做個(gè)記錄,此處暫對(duì)全局異常捕獲做個(gè)記錄)
static class Program
{
/// <summary>
/// 應(yīng)用程序的主入口點(diǎn)。
/// </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)用程序的主入口點(diǎn)
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
#endregion
}
catch (Exception ex)
{
string str = GetExceptionMsg(ex,string.Empty);
MessageBox.Show(str, "系統(tǒng)錯(cuò)誤", 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)錯(cuò)誤", 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)錯(cuò)誤", MessageBoxButtons.OK, MessageBoxIcon.Error);
//LogManager.WriteLog(str);
}
/// <summary>
/// 生成自定義異常消息
/// </summary>
/// <param name="ex">異常對(duì)象</param>
/// <param name="backStr">備用異常消息:當(dāng)ex為null時(shí)有效</param>
/// <returns>異常字符串文本</returns>
static string GetExceptionMsg(Exception ex,string backStr)
{
StringBuilder sb = new StringBuilder();
sb.AppendLine("****************************異常文本****************************");
sb.AppendLine("【出現(xiàn)時(shí)間】:" + 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)用程序的主入口點(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);
#region 應(yīng)用程序的主入口點(diǎn)
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)用程序線程錯(cuò)誤:{0}", ex);
}
//MessageBox.Show(str, "系統(tǒng)錯(cuò)誤", 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)用程序線程錯(cuò)誤:{0}", e);
}
//MessageBox.Show(str, "系統(tǒng)錯(cuò)誤", 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)錯(cuò)誤", MessageBoxButtons.OK, MessageBoxIcon.Error);
LogManager.WriteLog(str);
}
}
項(xiàng)目剛接手,所以打算做一個(gè)異常全局捕獲,統(tǒng)一處理的模式,采用具體詳細(xì)信息的對(duì)話框提醒與日志文件保存方式。以下是根據(jù)網(wǎng)上找的C#winform全局異常捕獲做了點(diǎn)修改。(等項(xiàng)目異常處理全部完成后,將心得體會(huì)做個(gè)記錄,此處暫對(duì)全局異常捕獲做個(gè)記錄)
復(fù)制代碼 代碼如下:
static class Program
{
/// <summary>
/// 應(yīng)用程序的主入口點(diǎn)。
/// </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)用程序的主入口點(diǎn)
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
#endregion
}
catch (Exception ex)
{
string str = GetExceptionMsg(ex,string.Empty);
MessageBox.Show(str, "系統(tǒng)錯(cuò)誤", 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)錯(cuò)誤", 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)錯(cuò)誤", MessageBoxButtons.OK, MessageBoxIcon.Error);
//LogManager.WriteLog(str);
}
/// <summary>
/// 生成自定義異常消息
/// </summary>
/// <param name="ex">異常對(duì)象</param>
/// <param name="backStr">備用異常消息:當(dāng)ex為null時(shí)有效</param>
/// <returns>異常字符串文本</returns>
static string GetExceptionMsg(Exception ex,string backStr)
{
StringBuilder sb = new StringBuilder();
sb.AppendLine("****************************異常文本****************************");
sb.AppendLine("【出現(xiàn)時(shí)間】:" + 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)用程序的主入口點(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);
#region 應(yīng)用程序的主入口點(diǎn)
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)用程序線程錯(cuò)誤:{0}", ex);
}
//MessageBox.Show(str, "系統(tǒng)錯(cuò)誤", 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)用程序線程錯(cuò)誤:{0}", e);
}
//MessageBox.Show(str, "系統(tǒng)錯(cuò)誤", 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)錯(cuò)誤", MessageBoxButtons.OK, MessageBoxIcon.Error);
LogManager.WriteLog(str);
}
}
相關(guān)文章
C#設(shè)計(jì)模式之Strategy策略模式解決007大破密碼危機(jī)問(wèn)題示例
這篇文章主要介紹了C#設(shè)計(jì)模式之Strategy策略模式解決007大破密碼危機(jī)問(wèn)題,簡(jiǎn)單描述了策略模式的定義并結(jié)合加密解密算法實(shí)例分析了C#策略模式的具體使用方法,需要的朋友可以參考下2017-09-09

C#中Request.Cookies 和 Response.Cookies 的區(qū)別分析
本文通過(guò)實(shí)例代碼向我們展示了C#中Request.Cookies 和 Response.Cookies 的區(qū)別,文章淺顯易懂,這里推薦給大家。
2014-11-11 
c#獲取當(dāng)前年的周數(shù)及當(dāng)前月的天數(shù)示例代碼
本篇文章主要是對(duì)c#獲取當(dāng)前年的周數(shù)及當(dāng)前月的天數(shù)示例代碼進(jìn)行了介紹,需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助
2014-01-01 
C#向數(shù)據(jù)庫(kù)中插入或更新null空值與延遲加載lazy
這篇文章介紹了C#向數(shù)據(jù)庫(kù)中插入或更新null空值與延遲加載lazy,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
2022-05-05