我怎样才能得到的WinForms停止默默地忽略未处理的异常? [英] How can I get WinForms to stop silently ignoring unhandled exceptions?

查看:186
本文介绍了我怎样才能得到的WinForms停止默默地忽略未处理的异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是越来越刺激性极强。现在我有一个WinForms应用程序,并且事不对的工作,但没有例外,正在就我能告诉抛出。通过几乎所有相关的code片步进后,事实证明,一个例外是在我的应用程序的启动被抛出。

This is getting extremely irritating. Right now I have a winforms application, and things were not working right, but no exceptions were being thrown as far as I could tell. After stepping through almost all pieces of relevant code, it turns out that an exception was being thrown at the start of my application.

长话短说,在的WinForms,是因为真棒,因为它是,如果发生异常的WinForms库忽略它。否发生未处理的异常JIT消息被抛出时,它只是停止处理当前事件,并返回到GUI。

Long story short, in WinForms, being as awesome as it is, if an exception occurs the WinForms library ignores it. No "an unhandled exception has occurred" JIT message is thrown, it just stops processing the current event and goes back to the GUI.

这是导致随机的错误,因为code加载数据不会被调用,由于异常之前加载这些数据的发生。

This is causing random bugs, because code to load data isn't being called due to the exception occurring prior to this data being loaded.

要看到这个动作我创建了一个全新的WinForms应用程序,并进入下code:

To see this in action I created a brand new WinForms application, and entered the following code:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        string blah = null;
        blah.Trim();
    }
}

preSS F5和形式的负载没有任何错误显示,即使空引用被抛出。

Press F5 and the form loads without any errors showing, even though a null reference is being thrown.

然后我试着去我的的Program.cs 的主要方法,并添加 Application.SetUnhandledExceptionMode(UnhandledExceptionMode.ThrowException); 它。还是我的窗体加载,而不会导致抛出任何错误。

I then tried to go to my Program.cs main method and add Application.SetUnhandledExceptionMode(UnhandledExceptionMode.ThrowException); to it. Still my form loads without causing any errors to be thrown.

虽然我知道,我可以告诉VS对所有异常突破,我认为这种情况非常糟糕。这会导致难以在生产调试真是奇怪的问题,因为这是一个内部工具我真的想这样,发生异常时,它实际上出现了错误拥有它,而不是默默忽略它。

Even though I know that I can tell VS to break on all exceptions, I find this situation really bad. It causes really wierd issues that are hard to debug in production, and as this is an internal tool I really want to have it so it actually errors out when an exception occurs, and not silently disregards it.

有谁知道如何做到这一点?

Does anyone know how to do this?



更新:刚刚更新的东西我从评论学会


Update: Just to update on things I have learned from the comments.

这确实出现了一个64位的问题与Windows,因为我从<有学问href=\"http://stackoverflow.com/questions/4933958/vs2010-does-not-show-unhandled-exception-message-in-a-64-bit-winforms-application\">this问题我没有发布之前看到的。在这个问题使它指向<一个href=\"https://connect.microsoft.com/VisualStudio/feedback/details/357311/silent-exceptions-on-x64-development-machines\">Microsoft关于这一点,这是这样说的bug报告:

This does appear to be a 64-bit issue with windows, as I learned from this question which I did not see before posting. In that question it pointed to a Microsoft bug report about this, which had this to say:

您好,

此bug已被关闭的外部,因为从64怎么样版本的Windows处理异常的这种行为的结果。当用户模式异常跨越内核过渡的Windows x64版本的不允许例外传播。因此,连接调试器不知道的事实发生导致调试异常未能对未处理的异常中断。

This bug was closed as "External" because this behavior results from how x64 version of Windows handle exceptions. When a user mode exception crosses a kernel transition, x64 versions of Windows do not allow the exception to propagate. Therefore attached debuggers are unaware of the fact that an exception occured resulting in the debugger failing to break on the unhandled exception.

不幸的是,其中没有什么是可视Studo团队可以做解决这个问题,它是操作系统设计的结果。所有关于这个问题,可以反馈给Windows团队;然而,Windows团队认为这是正确的操作系统设计,并考虑在x86行为是不正确。

Unfortunately where is nothing that the Visual Studo team can do to address this, it is the result of operating system design. All feedback regarding this issue should be addressed to the Windows team; however the Windows team considers this to be the "correct" operating system design, and considers the x86 behavior to be "incorrect".

最好的问候,
  Visual Studio调试器

Best Regards, Visual Studio Debugger

话虽这么说,建立不是通过Visual Studio(或使用Ctrl + F5运行)运行似乎显示JIT异常消息框的除非的,如果你有以下code你的Program.cs

That being said, builds not run through visual studio (or using Ctrl+F5 to run) does seem to show the JIT exception message box EXCEPT if you have the following code in your Program.cs:

Application.SetUnhandledExceptionMode(UnhandledExceptionMode.ThrowException);

这code会导致Windows忽略例外。

That code will cause windows to ignore the exception.

然而,如果你(不是)订阅 Application.ThreadException 事件,不仅将你的异常被捕获,Visual Studio的调试器的将会的打破未处理的异常!

However, if you (instead) subscribe to the Application.ThreadException event, not only will your exceptions be caught, visual studio's debugger will break on unhandled exceptions!

推荐答案

在您的Program.cs'主要功能,你也应该确保你的包裹中调用一个try / catch打开表单。另外使用 AppDomain.UnhandledException 来捕获异常。我们还添加了 Application.ThreadException 了。

In your Program.cs' Main function you should also ensure that you've wrapped your call to open the form in a try/catch. Additionally use the AppDomain.UnhandledException to catch exceptions. We also add Application.ThreadException too.

我相信下面会给你挂接到一个可以引发的所有异常...

I believe the following will give you hooks into all the exceptions that can be thrown...

static void Main()
{
    try
    {
        System.Windows.Forms.Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
        System.Windows.Forms.Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(OnGuiUnhandedException);
        AppDomain.CurrentDomain.UnhandledException += OnUnhandledException;

        var form = new MainForm();
        form.ShowDialog();
    }
    catch (Exception e)
    {
        HandleUnhandledException(e);
    }
    finally
    {
        // Do stuff
    }
}

private static void HandleUnhandledException(Object o)
{
    // TODO: Log it!
    Exception e = o as Exception;

    if (e != null)
    {

    }
}

private static void OnUnhandledException(Object sender, UnhandledExceptionEventArgs e)
{
    HandleUnhandledException(e.ExceptionObject);
}

private static void OnGuiUnhandedException(object sender, System.Threading.ThreadExceptionEventArgs e)
{
    HandleUnhandledException(e.Exception);
}

这篇关于我怎样才能得到的WinForms停止默默地忽略未处理的异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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