加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

C# 程序异常关闭时的捕获

发布时间:2020-12-16 01:13:26 所属栏目:百科 来源:网络整理
导读:本文主要以一个简单的小例子,描述C# Winform程序异常关闭时,如何进行捕获,并记录日志。 概述 有时在界面的事件中,明明有try... catch 进行捕获异常,但是还是会有异常关闭的情况,所以在程序中如何最终的记录一些无法捕获的异常,会大大方便问题的定位分

本文主要以一个简单的小例子,描述C# Winform程序异常关闭时,如何进行捕获,并记录日志。

概述

有时在界面的事件中,明明有try... catch 进行捕获异常,但是还是会有异常关闭的情况,所以在程序中如何最终的记录一些无法捕获的异常,会大大方便问题的定位分析及程序优化。

涉及知识点

以下两个异常事件,主要应用不同的场景。

  • Application.ThreadException?在发生应用程序UI主线程中未捕获线程异常时发生,触发的事件。
  • AppDomain.CurrentDomain.UnhandledException?当后台线程中某个异常未被捕获时触发。

主要源代码

主要程序(Program):

 1 using System;
 2  System.Collections.Generic;
 3  System.IO;
 4  System.Linq;
 5  System.Text;
 6  System.Threading.Tasks;
 7  System.Windows.Forms;
 8 
 9 namespace DemoException
10 {
11     static class Program
12     {
13         /// <summary>
14         /// 应用程序的主入口点。
15         </summary>
16         [STAThread]
17         void Main()
18         {
19             Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
20             //处理UI线程异常
21             Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
22             处理非线程异常
23             AppDomain.CurrentDomain.UnhandledException += UnhandledExceptionEventHandler(CurrentDomain_UnhandledException) ;
24             Application.EnableVisualStyles();
25             Application.SetCompatibleTextRenderingDefault(false);
26             Application.Run( FrmMain());
27             glExitApp = true;标志应用程序可以退出
28         }
29 
30         31          是否退出应用程序
32         33         bool glExitApp = ;
34 
35         36          处理未捕获异常
37         38         <param name="sender"></param>
39         <param name="e"></param>
40         private void CurrentDomain_UnhandledException(object sender,UnhandledExceptionEventArgs e)
41 42 
43             SaveLog("-----------------------begin--------------------------"44             SaveLog(CurrentDomain_UnhandledException"+DateTime.Now.ToString(yyyy-MM-dd hh:mm:ss));
45             SaveLog(IsTerminating : " + e.IsTerminating.ToString());
46             SaveLog(e.ExceptionObject.ToString());
47             SaveLog(-----------------------end----------------------------48             while (true)
49             {循环处理,否则应用程序将会退出
50                 if (glExitApp)
51                 {标志应用程序可以退出,否则程序退出后,进程仍然在运行
52                     SaveLog(ExitApp53                     return54                 }
55                 System.Threading.Thread.Sleep(2 * 100056             };
57 58 
59         60          处理UI主线程异常
61         62         63         64         void Application_ThreadException(65 66             SaveLog(67             SaveLog(Application_ThreadException:  e.Exception.Message);
68             SaveLog(e.Exception.StackTrace);
69             SaveLog(70 71 
72         public void SaveLog(string log)
73 74             string filePath =AppDomain.CurrentDomain.BaseDirectory+ @"objPerson.txt75             采用using关键字,会自动释放
76             using (FileStream fs =  FileStream(filePath,FileMode.Append))
77             {
78                 using (StreamWriter sw =  StreamWriter(fs,Encoding.Default))
79                 {
80                     sw.WriteLine(log);
81 82             }
83 84     }
85 }
View Code

出错的程序:

 System.ComponentModel;
 System.Data;
 System.Drawing;
 8  System.Threading;
11 
13 14     partial  FrmMain : Form
15 16 
public FrmMain()
            InitializeComponent();
20 21 
22         void FrmMain_Load(23 24            
25 26 
27         void btnTestUI_Click(29             int a = 030             int c = 10 / a;
31 32 
void btnTest2_Click(34 35             Thread t = new Thread(new ThreadStart(() =>
36 37                 38                 39             }));
40             t.IsBackground =             t.Start();
42 43 44 }
View Code

?下载链接

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读