码迷,mamicode.com
首页 > Windows程序 > 详细

WinForm C#全局错误捕捉处理【整理】

时间:2014-12-09 19:08:38      阅读:251      评论:0      收藏:0      [点我收藏+]

标签:winform   style   blog   io   ar   color   os   sp   for   

 1   static class Program
 2     {
 3         /// <summary>
 4         /// 应用程序的主入口点。
 5         /// </summary>
 6         [STAThread]
 7         static void Main()
 8         {
 9             try
10             {
11 
12                 //添加事件处理程序未捕获的异常   
13                 Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
14                 //添加事件处理UI线程异常   
15                 Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
16                 //添加事件处理非UI线程异常   
17                 AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
18 
19 
20                 Application.EnableVisualStyles();
21                 Application.SetCompatibleTextRenderingDefault(false);
22                 Application.Run(new FrmActivity());
23             }
24             catch (Exception ex)
25             {
26                 string str = "";
27                 string strDateInfo = "出现应用程序未处理的异常:" + DateTime.Now.ToString() + "\r\n";
28 
29                 if (ex != null)
30                 {
31                     str = string.Format(strDateInfo + "异常类型:{0}\r\n异常消息:{1}\r\n异常信息:{2}\r\n",
32                          ex.GetType().Name, ex.Message, ex.StackTrace);
33                 }
34                 else
35                 {
36                     str = string.Format("应用程序线程错误:{0}", ex);
37                 }
38 
39                 //写日志
40                 WriteLog.WriteErrLog(str);
41                 MessageBox.Show("发生致命错误,请及时联系作者!", "系统错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
42             }
43 
44         }
45 
46         /// <summary>
47         ///这就是我们要在发生未处理异常时处理的方法,做法很多,可以是把出错详细信息记录到文本、数据库,发送出错邮件到作者信箱或出错后重新初始化等等
48         /// </summary>
49         /// <param name="sender"></param>
50         /// <param name="e"></param>
51         static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
52         {
53 
54             string str = "";
55             string strDateInfo = "出现应用程序未处理的异常:" + DateTime.Now.ToString() + "\r\n";
56             Exception error = e.Exception as Exception;
57             if (error != null)
58             {
59                 str = string.Format(strDateInfo + "异常类型:{0}\r\n异常消息:{1}\r\n异常信息:{2}\r\n",
60                      error.GetType().Name, error.Message, error.StackTrace);
61             }
62             else
63             {
64                 str = string.Format("应用程序线程错误:{0}", e);
65             }
66             //写日志
67             WriteLog.WriteErrLog(str);
68             MessageBox.Show("发生致命错误,请及时联系作者!", "系统错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
69         }
70         /// <summary>
71         ///  ‘ 处理UI异常
72         /// </summary>
73         /// <param name="sender"></param>
74         /// <param name="e"></param>
75         static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
76         {
77             string str = "";
78             Exception error = e.ExceptionObject as Exception;
79             string strDateInfo = "出现应用程序未处理的异常:" + DateTime.Now.ToString() + "\r\n";
80             if (error != null)
81             {
82                 str = string.Format(strDateInfo + "Application UnhandledException:{0};\n\r堆栈信息:{1}", error.Message, error.StackTrace);
83             }
84             else
85             {
86                 str = string.Format("Application UnhandledError:{0}", e);
87             }
88             //写日志
89             WriteLog.WriteErrLog(str);
90             MessageBox.Show("发生致命错误,请停止当前操作并及时联系作者!", "系统错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
91         }
92 
93     }

 

WinForm C#全局错误捕捉处理【整理】

标签:winform   style   blog   io   ar   color   os   sp   for   

原文地址:http://www.cnblogs.com/thirst/p/4153798.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!