您的位置:首页 > 其它

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

2007-10-06 14:18 337 查看
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()之前添加
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: