在有的操作系統中winform程序出現異常的時候,並不會彈出異常對話框,而是直接退出了,沒有任何跡象,但是在系統的事件查看器(eventvwr.exe)中會發現這個異常。
為了能夠捕捉到程序的異常,我們需要加一個全局異常的捕捉代碼,當軟件無故退出的時候,這樣就能發現異常出現的具體位置。
網上有一些代碼,但是沒有說明具體的用法,有的是創建了一個異常捕捉類,在程序入口點去實例化。但是我們大多不願意去多創建這個類,這個時候只要在Program.cs文件中添加代碼去捕捉就行了,捕捉到了之后直接注釋掉,簡單,實用。
下面是整理過的:
在Program.cs中添加捕捉異常代碼,添加后的類如下
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Diagnostics;
using System.Threading;
using System.IO;
namespace logtest
{
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 Program.cs自動生成的代碼
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
// 程序入口點
Application.Run(new Form1());
#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);
// writeLog(str);
//}
}
#region 捕捉全局異常代碼
///// </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);
// }
// MessageBox.Show(str, "系統錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error);
// 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);
// writeLog(str);
//}
///// <summary>
///// 寫文件
///// </summary>
///// <param name="str"></param>
//static void writeLog(string str)
//{
// string ErrPath = AppDomain.CurrentDomain.BaseDirectory;
// if (!Directory.Exists(ErrPath))
// {
// Directory.CreateDirectory(ErrPath);
// }
// using (StreamWriter sw = new StreamWriter( @"D:\ErrLog.txt", true))
// {
// sw.WriteLine(str);
// sw.WriteLine("---------------------------------------------------------");
// sw.Close();
// }
//}
#endregion
上面加注釋的部分就是異常捕捉的代碼,當程序中任何位置出現異常時,會彈出對話框,報出異常的位置,大大方便了代碼的調試。
writeLog()方法是把異常的信息輸出到日志文件,我是不需要,直接在彈出的對話框中就可以看見異常的位置了,不過可是使用,注意要保證路徑正確並且有寫入的權限。