C#程序異常關(guān)閉時(shí)的捕獲
本文主要以一個(gè)簡(jiǎn)單的小例子,描述C# Winform程序異常關(guān)閉時(shí),如何進(jìn)行捕獲,并記錄日志。
概述
有時(shí)在界面的事件中,明明有try... catch 進(jìn)行捕獲異常,但是還是會(huì)有異常關(guān)閉的情況,所以在程序中如何最終的記錄一些無(wú)法捕獲的異常,會(huì)大大方便問(wèn)題的定位分析及程序優(yōu)化。
涉及知識(shí)點(diǎn)
以下兩個(gè)異常事件,主要應(yīng)用不同的場(chǎng)景。
- Application.ThreadException 在發(fā)生應(yīng)用程序UI主線程中未捕獲線程異常時(shí)發(fā)生,觸發(fā)的事件。
- AppDomain.CurrentDomain.UnhandledException 當(dāng)后臺(tái)線程中某個(gè)異常未被捕獲時(shí)觸發(fā)。
源代碼
主要程序(Program):
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace DemoException { static class Program { /// <summary> /// 應(yīng)用程序的主入口點(diǎn)。 /// </summary> [STAThread] static void Main() { Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException); //處理UI線程異常 Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException); //處理非線程異常 AppDomain.CurrentDomain.UnhandledException +=new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException) ; Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new FrmMain()); glExitApp = true;//標(biāo)志應(yīng)用程序可以退出 } /// <summary> /// 是否退出應(yīng)用程序 /// </summary> static bool glExitApp = false; /// <summary> /// 處理未捕獲異常 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) { SaveLog("-----------------------begin--------------------------"); SaveLog("CurrentDomain_UnhandledException"+DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss")); SaveLog("IsTerminating : " + e.IsTerminating.ToString()); SaveLog(e.ExceptionObject.ToString()); SaveLog("-----------------------end----------------------------"); while (true) {//循環(huán)處理,否則應(yīng)用程序?qū)?huì)退出 if (glExitApp) {//標(biāo)志應(yīng)用程序可以退出,否則程序退出后,進(jìn)程仍然在運(yùn)行 SaveLog("ExitApp"); return; } System.Threading.Thread.Sleep(2 * 1000); }; } /// <summary> /// 處理UI主線程異常 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e) { SaveLog("-----------------------begin--------------------------"); SaveLog("Application_ThreadException:" + e.Exception.Message); SaveLog(e.Exception.StackTrace); SaveLog("-----------------------end----------------------------"); } public static void SaveLog(string log) { string filePath =AppDomain.CurrentDomain.BaseDirectory+ @"\objPerson.txt"; //采用using關(guān)鍵字,會(huì)自動(dòng)釋放 using (FileStream fs = new FileStream(filePath, FileMode.Append)) { using (StreamWriter sw = new StreamWriter(fs, Encoding.Default)) { sw.WriteLine(log); } } } } }
出錯(cuò)的程序:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; namespace DemoException { public partial class FrmMain : Form { public FrmMain() { InitializeComponent(); } private void FrmMain_Load(object sender, EventArgs e) { } private void btnTestUI_Click(object sender, EventArgs e) { int a = 0; int c = 10 / a; } private void btnTest2_Click(object sender, EventArgs e) { Thread t = new Thread(new ThreadStart(() => { int a = 0; int c = 10 / a; })); t.IsBackground = true; t.Start(); } } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
WPF+ASP.NET SignalR實(shí)現(xiàn)動(dòng)態(tài)折線圖的繪制
這篇文章將以一個(gè)簡(jiǎn)單的動(dòng)態(tài)折線圖示例,簡(jiǎn)述如何通過(guò)ASP.NET SignalR實(shí)現(xiàn)后臺(tái)通知功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2023-01-01C#使用QRCode生成海報(bào)圖并嵌入定位帶logo的二維碼
這篇文章主要為大家詳細(xì)介紹了C#如何使用QRCode生成海報(bào)圖并嵌入定位帶logo的二維碼,文中的示例代碼講解詳細(xì),需要的小伙伴可以參考下2024-03-03C# Char結(jié)構(gòu)中IsLetterOrDigit(Char)的方法詳解
這篇文章給大家介紹了C#的Char 結(jié)構(gòu)的IsLetterOrDigit(Char)的方法,并通過(guò)代碼示例給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2024-02-02C#中使用JSON.NET實(shí)現(xiàn)JSON、XML相互轉(zhuǎn)換
這篇文章主要介紹了C#中使用JSON.NET實(shí)現(xiàn)JSON、XML相互轉(zhuǎn)換的相關(guān)代碼及示例,需要的朋友可以參考下2015-11-11C#微信公眾號(hào)開(kāi)發(fā) 微信事件交互
這篇文章主要介紹了C#微信公眾號(hào)開(kāi)發(fā),微信事件交互的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01c#圖片縮放圖片剪切功能實(shí)現(xiàn)(等比縮放)
c#圖片縮放剪切功能實(shí)現(xiàn),代碼中包含了c#圖片處理的一些基礎(chǔ)知識(shí),與大家分享2013-12-12