winform中捕获程序未处理的所有异常

1、命名空间引用:

using System.Text;

2、处理方法:

           private static void AppThreadException(object source, System.Threading.ThreadExceptionEventArgs e)
           {
               string errorMsg = string.Format("未处理异常: \n{0}\n", e.Exception.Message);
               errorMsg += Environment.NewLine;

               DialogResult result = MessageBox.Show(errorMsg, "Application Error", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Stop);

               //如果点击“中止”则退出程序
               if (result == DialogResult.Abort)
               {
                   Application.Exit();
               }
           }

3、Main方法添加:
          Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(AppThreadException);  

在Application.Run()之前添加

原文地址:https://www.cnblogs.com/tuyile006/p/915367.html