Winforms应用程序中未处理的异常 [英] Unhandled Exception in Winforms Application

查看:155
本文介绍了Winforms应用程序中未处理的异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的WinForms应用程序用于输入测试用例。自从我将该应用程序升级到.NET 4.0之后,并向标签页控件添加了一个新的标签页,用于根据XSD架构验证XML,应用程序已经随机崩溃。我无法重现异常。



我的质量检查员收到的错误是通用的Windows消息:



TestCaseViewer遇到问题,需要关闭。对于由此造成的不便,我们深表歉意。



为了尝试找到真正的错误,我将以下代码添加到主程序的开头:

  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 = (例外)e.ExceptionObject;
MessageBox.Show(ex.ToString(),@未处理的异常,
MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
}
finally
{
Application.Exit();
}
}

不幸的是,这没有帮助,错误继续这样做,以产生冒泡到操作系统的未处理的错误。



任何人都可以给我任何关于捕获此异常的其他想法? p>

解决方案

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

 <运行时间> 
<! - 以下设置可防止主机在抛出未处理的异常时关闭 - >
< legacyUnhandledExceptionPolicy enabled =1/>
< / runtime>


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?

解决方案

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>

这篇关于Winforms应用程序中未处理的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆