Loa*_*ian 6 unhandled exception winforms
我有一个简单的WinForms应用程序,用于输入测试用例.自从我将此应用程序升级到.NET 4.0并向标签页控件添加了一个新的标签页以便针对XSD架构验证XML之后,应用程序一直在随机崩溃.我一直无法重现异常.
我的QA人收到的错误是通用的Windows消息:
TestCaseViewer遇到问题,需要关闭.我们对造成的不便很抱歉.
为了尝试找到真正的错误,我将以下代码添加到Main方法的开头:
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
Application.ThreadException += Application_ThreadException;
Run Code Online (Sandbox Code Playgroud)
事件处理程序如下所示:
static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
{
try
{
MessageBox.Show(e.Exception.ToString(), @"Thread Exception",
MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
finally
{
Application.Exit();
}
}
static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
try
{
var ex = (Exception)e.ExceptionObject;
MessageBox.Show(ex.ToString(), @"Unhandled Exception",
MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
finally
{
Application.Exit();
}
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,这没有任何帮助,无论如何,错误继续以一种产生未处理的错误的方式继续这样做,这种错误正在冒泡到操作系统.
任何人都可以给我任何关于捕获此异常的其他想法吗?
小智 10
尝试将以下内容添加到app.config中
<runtime>
<!-- the following setting prevents the host from closing when an unhandled exception is thrown -->
<legacyUnhandledExceptionPolicy enabled="1" />
</runtime>
Run Code Online (Sandbox Code Playgroud)
如果您使用的是 Visual Studio,则可以将其设置为在所有未处理的异常上中断,甚至在抛出异常时中断,无论您的代码是否处理该异常。
为此,请从“调试”菜单中选择“异常”。您将看到一个如下所示的对话框:

如果你真的想认真对待,请尝试勾选所有选项。然后,从您的 QA 人员那里了解触发异常的具体操作,并在开发环境中的调试器下运行时准确地重现这些操作。每当抛出异常时,Visual Studio 就会中断,您将看到有问题的代码行以及完整的堆栈跟踪。