Winforms 应用程序中未处理的异常
我有一个简单的 WinForms 应用程序,用于输入测试用例。自从我将此应用程序升级到 .NET 4.0 并向选项卡页控件添加一个新选项卡页以根据 XSD 架构验证 XML 以来,该应用程序一直在随机崩溃。我无法重现该异常。
我的 QA 人员收到的错误是通用的 Windows 消息:
TestCaseViewer 遇到问题,需要关闭。对于给您带来的不便,我们深表歉意。
为了尝试找到真正的错误,我在程序的 Main 方法的开头添加了以下代码:
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
Application.ThreadException += Application_ThreadException;
事件处理程序如下所示:
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();
}
}
不幸的是,这没有帮助,无论发生什么错误,都会继续在生成未处理的错误并冒泡到操作系统的方式。
谁能给我关于捕获此异常的任何其他想法?
I have a simple WinForms app that is used to enter test cases. Ever since I upgraded this application to .NET 4.0 and added a new tab page to the tab page control for validating XML against XSD schema the application has been randomly crashing. I've been unable to reproduce the exception.
The error my QA guy receives is the generic Windows message:
TestCaseViewer has encountered a problem and needs to close. We are sorry for the inconvenience.
To try to get to the real error I've added the following code to the beginning of the Main method of program:
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
Application.ThreadException += Application_ThreadException;
The event handlers look like this:
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();
}
}
Unfortunately this hasn't helped and whatever is thowing the error continues to do so in a way that generates the unhandled error that is bubbling up to the OS.
Can anyone give me any other ideas about trapping this exception?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试将以下内容添加到您的 app.config
Try adding the following to your app.config
如果您使用的是 Visual Studio,则可以将其设置为在所有未处理的异常上中断,甚至在抛出异常时中断,无论是否处理该异常通过你的代码。
为此,请从“调试”菜单中选择“异常”。您将看到一个如下所示的对话框:
如果您确实想要获得严重的是,尝试检查所有的方框。然后,从您的 QA 人员那里了解触发异常的具体操作,并在开发环境中的调试器下运行时准确地重现这些操作。每当抛出异常时,Visual Studio 就会中断,您将看到有问题的代码行以及完整的堆栈跟踪。
If you're using Visual Studio, you can set it to break on all unhandled exceptions and even any time that an exception is thrown, regardless of whether or not it is handled by your code.
To do this, select "Exceptions" from the "Debug" menu. You'll get a dialog box that looks like this:
If you really want to get serious, try checking all of the boxes. Then, find out from your QA guy what exactly is being done to trigger the exception, and reproduce those actions exactly while running under the debugger within the development environment. Whenever the exception is thrown, Visual Studio will break and you'll see the offending line of code along with a complete stack trace.