Winforms 应用程序中未处理的异常

发布于 2024-10-18 01:53:13 字数 1393 浏览 2 评论 0原文

我有一个简单的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小猫一只 2024-10-25 01:53:13

尝试将以下内容添加到您的 app.config

<runtime>
   <!-- the following setting prevents the host from closing when an unhandled exception is thrown -->
   <legacyUnhandledExceptionPolicy enabled="1" />
</runtime>

Try adding the following to your app.config

<runtime>
   <!-- the following setting prevents the host from closing when an unhandled exception is thrown -->
   <legacyUnhandledExceptionPolicy enabled="1" />
</runtime>
<逆流佳人身旁 2024-10-25 01:53:13

如果您使用的是 Visual Studio,则可以将其设置为在所有未处理的异常上中断,甚至在抛出异常时中断,无论是否处理该异常通过你的代码。

为此,请从“调试”菜单中选择“异常”。您将看到一个如下所示的对话框:

  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:

  Visual Studio Exceptions dialog

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文