WinForm程序统一捕捉自定义异常

1:新建自定义异常类

 1  /// <summary>
 2     /// 业务异常类
 3     /// </summary>
 4     public class BusinessException : Exception
 5     {
 6         public BusinessException()
 7         {
 8         }
 9         public BusinessException(string message): base(message)
10         {
11         }
12         public BusinessException(string message, System.Exception inner)
13             : base(message, inner)
14         {
15         }
16         public BusinessException(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context): base(info, context)
17         {
18         }
19     }
View Code

2:在Program文件中捕捉异常

 1 /// <summary>
 2         /// 应用程序的主入口点。
 3         /// </summary>
 4         [STAThread]
 5         static void Main()
 6         {
 7             try
 8             {
 9                 //处理未捕获的异常
10                 Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
11                 //处理UI线程异常
12                 Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
13                 //处理非UI线程异常
14                 AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
15                 Application.EnableVisualStyles();
16                 Application.SetCompatibleTextRenderingDefault(false);
17                 Application.Run(new Form1());
18             }
19             catch (Exception ex)
20             {
21                 string str = "";
22                 string strDateInfo = "出现应用程序未处理的异常:" + DateTime.Now.ToString() + "
";
23                 if (ex != null)
24                 {
25                     str = string.Format(strDateInfo + "异常类型:{0}
异常消息:{1}
异常信息:{2}
",
26                     ex.GetType().Name, ex.Message, ex.StackTrace);
27                 }
28                 else
29                 {
30                     str = string.Format("应用程序线程错误:{0}", ex);
31                 }
32                 
33                 MessageBox.Show("发生致命错误,请及时联系作者!"+str, "系统错误", 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             string str = "";
46             string strDateInfo = "出现应用程序未处理的异常:" + DateTime.Now.ToString() + "
";
47             Exception error = e.Exception as Exception;
48             if (error != null)
49             {
50                 str = string.Format(strDateInfo + "异常类型:{0}
异常消息:{1}
异常信息:{2}
",
51                 error.GetType().Name, error.Message, error.StackTrace);
52             }
53             else
54             {
55                 str = string.Format("应用程序线程错误:{0}", e);
56             }
57            
58             MessageBox.Show("发生致命错误,请及时联系作者!"+ str, "系统错误"+ str, MessageBoxButtons.OK, MessageBoxIcon.Warning);
59         }
60         static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
61         {
62             string str = "";
63             Exception error = e.ExceptionObject as Exception;
64             string strDateInfo = "出现应用程序未处理的异常:" + DateTime.Now.ToString() + "
";
65             if (error != null)
66             {
67                 str = string.Format(strDateInfo + "Application UnhandledException:{0};

堆栈信息:{1}", error.Message, error.StackTrace);
68             }
69             else
70             {
71                 str = string.Format("Application UnhandledError:{0}", e);
72             }
73             
74             MessageBox.Show("发生致命错误,请停止当前操作并及时联系作者!"+ str, "系统错误", MessageBoxButtons.OK, MessageBoxIcon.Warning);
75         }
76        
View Code
原文地址:https://www.cnblogs.com/baiyundadie/p/11189686.html