国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

C#程序異常關閉時的捕獲

2019-10-29 21:01:16
字體:
來源:轉載
供稿:網友

本文主要以一個簡單的小例子,描述C# Winform程序異常關閉時,如何進行捕獲,并記錄日志。

概述

有時在界面的事件中,明明有try... catch 進行捕獲異常,但是還是會有異常關閉的情況,所以在程序中如何最終的記錄一些無法捕獲的異常,會大大方便問題的定位分析及程序優化。

涉及知識點

以下兩個異常事件,主要應用不同的場景。

  • Application.ThreadException 在發生應用程序UI主線程中未捕獲線程異常時發生,觸發的事件。
  • AppDomain.CurrentDomain.UnhandledException 當后臺線程中某個異常未被捕獲時觸發。

源代碼

主要程序(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>    /// 應用程序的主入口點。    /// </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;//標志應用程序可以退出    }    /// <summary>    /// 是否退出應用程序    /// </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)      {//循環處理,否則應用程序將會退出        if (glExitApp)        {//標志應用程序可以退出,否則程序退出后,進程仍然在運行          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關鍵字,會自動釋放      using (FileStream fs = new FileStream(filePath, FileMode.Append))      {        using (StreamWriter sw = new StreamWriter(fs, Encoding.Default))        {          sw.WriteLine(log);        }      }    }  }}

出錯的程序:

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();    }  }}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。


注:相關教程知識閱讀請移步到c#教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 民乐县| 常宁市| 合阳县| 英德市| 梁平县| 始兴县| 微山县| 无为县| 温宿县| 鄢陵县| 旺苍县| 天水市| 武城县| 红安县| 建水县| 海晏县| 阳原县| 错那县| 霍林郭勒市| 呈贡县| 遂平县| 光山县| 红原县| 全椒县| 阜新市| 云龙县| 河北省| 荃湾区| 肥西县| 左云县| 美姑县| 安岳县| 藁城市| 玉门市| 安康市| 遂溪县| 峨山| 庆云县| 揭东县| 景宁| 濉溪县|