C# WinForm捕捉未處置的異常實例解析。本站提示廣大學習愛好者:(C# WinForm捕捉未處置的異常實例解析)文章只能為提供參考,不一定能成為您想要的結果。以下是C# WinForm捕捉未處置的異常實例解析正文
本文以一個完全的實例情勢講述了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#法式設計有所贊助。