C# WinForm捕获未处理的异常实例解析
本文以一个完整的实例形式讲述了C#WinForm捕获未处理的异常的方法。分享给大家供大家参考之用。具体代码如下:
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Windows.Forms;
usingSystem.IO;
namespaceGobalException
{
staticclassProgram
{
///<summary>
///应用程序的主入口点。
///</summary>
[STAThread]
staticvoidMain()
{
try
{
//处理未捕获的异常
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
//处理UI线程异常
Application.ThreadException+=newSystem.Threading.ThreadExceptionEventHandler(Application_ThreadException);
//处理非UI线程异常
AppDomain.CurrentDomain.UnhandledException+=newUnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(newForm1());
}
catch(Exceptionex)
{
stringstr="";
stringstrDateInfo="出现应用程序未处理的异常:"+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);
//frmBugf=newfrmBug(str);//友好提示界面
//f.ShowDialog();
MessageBox.Show("发生致命错误,请及时联系作者!","系统错误",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
}
///<summary>
///这就是我们要在发生未处理异常时处理的方法,我这是写出错详细信息到文本,如出错后弹出一个漂亮的出错提示窗体,给大家做个参考
///做法很多,可以是把出错详细信息记录到文本、数据库,发送出错邮件到作者信箱或出错后重新初始化等等
///这就是仁者见仁智者见智,大家自己做了。
///</summary>
///<paramname="sender"></param>
///<paramname="e"></param>
staticvoidApplication_ThreadException(objectsender,System.Threading.ThreadExceptionEventArgse)
{
stringstr="";
stringstrDateInfo="出现应用程序未处理的异常:"+DateTime.Now.ToString()+"\r\n";
Exceptionerror=e.ExceptionasException;
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);
//frmBugf=newfrmBug(str);//友好提示界面
//f.ShowDialog();
MessageBox.Show("发生致命错误,请及时联系作者!","系统错误",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
staticvoidCurrentDomain_UnhandledException(objectsender,UnhandledExceptionEventArgse)
{
stringstr="";
Exceptionerror=e.ExceptionObjectasException;
stringstrDateInfo="出现应用程序未处理的异常:"+DateTime.Now.ToString()+"\r\n";
if(error!=null)
{
str=string.Format(strDateInfo+"ApplicationUnhandledException:{0};\n\r堆栈信息:{1}",error.Message,error.StackTrace);
}
else
{
str=string.Format("ApplicationUnhandledError:{0}",e);
}
writeLog(str);
//frmBugf=newfrmBug(str);//友好提示界面
//f.ShowDialog();
MessageBox.Show("发生致命错误,请停止当前操作并及时联系作者!","系统错误",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
///<summary>
///写文件
///</summary>
///<paramname="str"></param>
staticvoidwriteLog(stringstr)
{
if(!Directory.Exists("ErrLog"))
{
Directory.CreateDirectory("ErrLog");
}
using(StreamWritersw=newStreamWriter(@"ErrLog\ErrLog.txt",true))
{
sw.WriteLine(str);
sw.WriteLine("---------------------------------------------------------");
sw.Close();
}
}
}
}
本文实例配有较为详尽的注释,便于大家阅读理解。希望本文所述对大家的C#程序设计有所帮助。
热门推荐
10 五一公司放假祝福语简短
11 追星女孩的祝福语简短
12 给长辈端午祝福语 简短
13 生日祝福语有诗意简短
14 团圆日祝福语简短
15 中秋商务祝福语简短最新
16 温暖文艺简短祝福语短句
17 男孩上学祝福语简短的
18 入住酒店文案祝福语简短