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

it2022-05-09  36

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/archive/2007/10/06/915367.html

相关资源:数据结构—成绩单生成器

最新回复(0)