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

捕获异常 winform

时间:2015-07-21 18:33:15      阅读:304      评论:0      收藏:0      [点我收藏+]

标签:

可以捕获winform中的异常写到文本中

技术分享
 1  [STAThread]
 2         static void Main()
 3         {
 4             try
 5             {
 6                 //处理未捕获的异常  
 7                 Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
 8                 //处理UI线程异常  
 9                 Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
10                 //处理非UI线程异常  
11                 AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
12                 Application.EnableVisualStyles();
13                 Application.SetCompatibleTextRenderingDefault(false);
14                 Application.Run(new Form1());
15             }
16             catch (Exception ex)
17             {
18                 string str = "";
19                 string strDateInfo = "出现应用程序未处理的异常:" + DateTime.Now.ToString() + "\r\n";
20                 if (ex != null)
21                 {
22                     str = string.Format(strDateInfo + "异常类型:{0}\r\n异常消息:{1}\r\n异常信息:{2}\r\n",
23                        ex.GetType().Name, ex.Message, ex.StackTrace);
24                 }
25                 else
26                 {
27                     str = string.Format("应用程序线程错误:{0}", ex);
28                 }
29 
30                 writeLog(str);
31                 //frmBug f = new frmBug(str);//友好提示界面
32                 //f.ShowDialog();
33                 MessageBox.Show("发生致命错误,请及时联系作者!", "系统错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
34             }
35         }
36         /// <summary>
37         ///这就是我们要在发生未处理异常时处理的方法,我这是写出错详细信息到文本,如出错后弹出一个漂亮的出错提示窗体,给大家做个参考
38         ///做法很多,可以是把出错详细信息记录到文本、数据库,发送出错邮件到作者信箱或出错后重新初始化等等
39         ///这就是仁者见仁智者见智,大家自己做了。
40         /// </summary>
41         /// <param name="sender"></param>
42         /// <param name="e"></param>
43         static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
44         {
45 
46             string str = "";
47             string strDateInfo = "出现应用程序未处理的异常:" + DateTime.Now.ToString() + "\r\n";
48             Exception error = e.Exception as Exception;
49             if (error != null)
50             {
51                 str = string.Format(strDateInfo + "异常类型:{0}\r\n异常消息:{1}\r\n异常信息:{2}\r\n",
52                    error.GetType().Name, error.Message, error.StackTrace);
53             }
54             else
55             {
56                 str = string.Format("应用程序线程错误:{0}", e);
57             }
58             writeLog(str);
59             //frmBug f = new frmBug(str);//友好提示界面
60             //f.ShowDialog();
61             MessageBox.Show("发生致命错误,请及时联系作者!", "系统错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
62         }
63         static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
64         {
65             string str = "";
66             Exception error = e.ExceptionObject as Exception;
67             string strDateInfo = "出现应用程序未处理的异常:" + DateTime.Now.ToString() + "\r\n";
68             if (error != null)
69             {
70                 str = string.Format(strDateInfo + "Application UnhandledException:{0};\n\r堆栈信息:{1}", error.Message, error.StackTrace);
71             }
72             else
73             {
74                 str = string.Format("Application UnhandledError:{0}", e);
75             }
76             writeLog(str);
77             //frmBug f = new frmBug(str);//友好提示界面
78             //f.ShowDialog();
79             MessageBox.Show("发生致命错误,请停止当前操作并及时联系作者!", "系统错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
80         }
81         /// <summary>
82         /// 写文件
83         /// </summary>
84         /// <param name="str"></param>
85         static void writeLog(string str)
86         {
87             if (!Directory.Exists("ErrLog"))
88             {
89                 Directory.CreateDirectory("ErrLog");
90             }
91             
92             using (StreamWriter sw = new StreamWriter(@"ErrLog\ErrLog-"+DateTime.Now.ToString("yyyy-MM-dd")+".txt", true, System.Text.Encoding.UTF8))
93             {
94                 sw.WriteLine(str);
95                 sw.WriteLine("---------------------------------------------------------");
96                 
97                 sw.Close();
98             }
99         }
program

这个在program中的文件,程序的入口

捕获异常 winform

标签:

原文地址:http://www.cnblogs.com/zhaokunbokeyuan256/p/4665274.html

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