C# 捕获系统闪退BUG
在程序入口添加代碼?
namespace MainForm
{
? ? static class Program
? ? {
? ? ? ? /// <summary>
? ? ? ? /// 應用程序的主入口點。
? ? ? ? /// </summary>
? ? ? ? [STAThread]
? ? ? ? static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
? ? ? ? {
? ? ? ? ? ? MessageBox.Show(e.ExceptionObject.ToString());
? ? ? ? }
? ? ? ? static void Main()
? ? ? ? {
? ? ? ? ? ? try
? ? ? ? ? ? {
? ? ? ? ? ? ? ? AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
? ? ? ? ? ? ? ??
? ? ? ? ? ? }
? ? ? ? ? ? catch(Exception e)
? ? ? ? ? ? {
?
? ? ? ? ? ? }
? ? ? ? ? ? Application.EnableVisualStyles();
? ? ? ? ? ? Application.SetCompatibleTextRenderingDefault(false);
? ? ? ? ? ? Application.Run(new Form1());
? ? ? ? }
? ? }
}
第二個方法:?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Text;
using log4net;
?
namespace TestLog4Net
{
? ? static class Program
? ? {
? ? ? ? /// <summary>
? ? ? ? /// 應用程序的主入口點。
? ? ? ? /// </summary>
? ? ? ? [STAThread]
? ? ? ? static void Main()
? ? ? ? {
? ? ? ? ? ? BindExceptionHandler();//綁定程序中的異常處理
? ? ? ? ? ? Application.EnableVisualStyles();
? ? ? ? ? ? Application.SetCompatibleTextRenderingDefault(false);
? ? ? ? ? ? Application.Run(new Form1());
? ? ? ? }
? ? ? ? /// <summary>
? ? ? ? /// 綁定程序中的異常處理
? ? ? ? /// </summary>
? ? ? ? private static void BindExceptionHandler()
? ? ? ? {
? ? ? ? ? ? //設置應用程序處理異常方式:ThreadException處理
? ? ? ? ? ? Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
? ? ? ? ? ? //處理UI線程異常
? ? ? ? ? ? Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
? ? ? ? ? ? //處理未捕獲的異常
? ? ? ? ? ? AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
? ? ? ? }
? ? ? ? /// <summary>
? ? ? ? /// 處理UI線程異常
? ? ? ? /// </summary>
? ? ? ? /// <param name="sender"></param>
? ? ? ? /// <param name="e"></param>
? ? ? ? static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
? ? ? ? {
? ? ? ? ? ? MessageBox.Show(e.Exception.ToString());
? ? ? ? }
? ? ? ? /// <summary>
? ? ? ? /// 處理未捕獲的異常
? ? ? ? /// </summary>
? ? ? ? /// <param name="sender"></param>
? ? ? ? /// <param name="e"></param>
? ? ? ? static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
? ? ? ? {
? ? ? ? ? ? MessageBox.Show(e.ExceptionObject.ToString());
?
//**************
1 static class Program2 {3 /// <summary>4 /// 應用程序的主入口點。5 /// </summary>6 [STAThread]7 static void Main()8 {9 try 10 { 11 //設置應用程序處理異常方式:ThreadException處理 12 Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException); 13 //處理UI線程異常 14 Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException); 15 //處理非UI線程異常 16 AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException); 17 18 #region 應用程序的主入口點 19 Application.EnableVisualStyles(); 20 Application.SetCompatibleTextRenderingDefault(false); 21 Application.Run(new Form1()); 22 #endregion 23 } 24 catch (Exception ex) 25 { 26 string str = GetExceptionMsg(ex,string.Empty); 27 MessageBox.Show(str, "系統錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error); 28 } 29 } 30 31 32 static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e) 33 { 34 string str = GetExceptionMsg(e.Exception, e.ToString()); 35 MessageBox.Show(str, "系統錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error); 36 //LogManager.WriteLog(str); 37 } 38 39 static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) 40 { 41 string str = GetExceptionMsg(e.ExceptionObject as Exception, e.ToString()); 42 MessageBox.Show(str, "系統錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error); 43 //LogManager.WriteLog(str); 44 } 45 46 /// <summary> 47 /// 生成自定義異常消息 48 /// </summary> 49 /// <param name="ex">異常對象</param> 50 /// <param name="backStr">備用異常消息:當ex為null時有效</param> 51 /// <returns>異常字符串文本</returns> 52 static string GetExceptionMsg(Exception ex,string backStr) 53 { 54 StringBuilder sb = new StringBuilder(); 55 sb.AppendLine("****************************異常文本****************************"); 56 sb.AppendLine("【出現時間】:" + DateTime.Now.ToString()); 57 if (ex != null) 58 { 59 sb.AppendLine("【異常類型】:" + ex.GetType().Name); 60 sb.AppendLine("【異常信息】:" + ex.Message); 61 sb.AppendLine("【堆棧調用】:" + ex.StackTrace); 62 } 63 else 64 { 65 sb.AppendLine("【未處理異常】:" + backStr); 66 } 67 sb.AppendLine("***************************************************************"); 68 return sb.ToString(); 69 } 70 }?
參考:
1 static class Program2 {3 /// <summary>4 /// 應用程序的主入口點。5 /// </summary>6 [STAThread]7 static void Main()8 {9 try 10 { 11 //處理未捕獲的異常 12 Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException); 13 //處理UI線程異常 14 Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException); 15 //處理非UI線程異常 16 AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException); 17 18 #region 應用程序的主入口點 19 20 Application.EnableVisualStyles(); 21 Application.SetCompatibleTextRenderingDefault(false); 22 Application.Run(new Main()); 23 24 #endregion 25 26 } 27 catch (Exception ex) 28 { 29 string str = ""; 30 string strDateInfo = "出現應用程序未處理的異常:" + DateTime.Now.ToString() + "\r\n"; 31 32 if (ex != null) 33 { 34 str = string.Format(strDateInfo + "異常類型:{0}\r\n異常消息:{1}\r\n異常信息:{2}\r\n", 35 ex.GetType().Name, ex.Message, ex.StackTrace); 36 } 37 else 38 { 39 str = string.Format("應用程序線程錯誤:{0}", ex); 40 } 41 42 //MessageBox.Show(str, "系統錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error); 43 LogManager.WriteLog(str); 44 } 45 46 } 47 48 49 static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e) 50 { 51 string str = ""; 52 string strDateInfo = "出現應用程序未處理的異常:" + DateTime.Now.ToString() + "\r\n"; 53 Exception error = e.Exception as Exception; 54 if (error != null) 55 { 56 str = string.Format(strDateInfo + "異常類型:{0}\r\n異常消息:{1}\r\n異常信息:{2}\r\n", 57 error.GetType().Name, error.Message, error.StackTrace); 58 } 59 else 60 { 61 str = string.Format("應用程序線程錯誤:{0}", e); 62 } 63 64 //MessageBox.Show(str, "系統錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error); 65 LogManager.WriteLog(str); 66 } 67 68 static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) 69 { 70 string str = ""; 71 Exception error = e.ExceptionObject as Exception; 72 string strDateInfo = "出現應用程序未處理的異常:" + DateTime.Now.ToString() + "\r\n"; 73 if (error != null) 74 { 75 str = string.Format(strDateInfo + "Application UnhandledException:{0};\n\r堆棧信息:{1}", error.Message, error.StackTrace); 76 } 77 else 78 { 79 str = string.Format("Application UnhandledError:{0}", e); 80 } 81 82 //MessageBox.Show(str, "系統錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error); 83 LogManager.WriteLog(str); 84 } 85 }總結
以上是生活随笔為你收集整理的C# 捕获系统闪退BUG的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: winform程序最大化时,页面显示不全
- 下一篇: c# char unsigned_dll