AppDomain.CurrentDomain.UnhandledException无需调试即可触发 [英] AppDomain.CurrentDomain.UnhandledException not firing without debugging

查看:336
本文介绍了AppDomain.CurrentDomain.UnhandledException无需调试即可触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个.NET程序,一个事件处理程序绑定到Application.CurrentDomain.UnhandledException。
运行调试程序时,当引发未处理的异常时,会触发该事件。但是,当没有调试运行时,事件不会触发。



我的问题是什么?



谢谢,
Andrew

解决方案

我假设您没有设置正确的异常处理模式,使用 - 只需将其设置为 UnhandledExceptionMode.ThrowException



更新


$ b $我刚刚写了一个小测试应用程序,发现没有什么可以工作的。您可以尝试使用此测试代码重现您的错误吗?

  using System; 
使用System.Drawing;
使用System.Threading;
使用System.Windows.Forms;

命名空间ConsoleApplication
{
public static class Program
{
static void Main()
{
AppDomain.CurrentDomain。 UnhandledException + = AppDomain_UnhandledException;

Application.ThreadException + = Application_ThreadException;
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);

Application.Run(new TestForm());

抛出新异常(Main);
}

static void Application_ThreadException(Object sender,ThreadExceptionEventArgs e)
{
MessageBox.Show(e.Exception.Message,Application.ThreadException);
}

static void AppDomain_UnhandledException(Object sender,UnhandledExceptionEventArgs e)
{
MessageBox.Show(((Exception)e.ExceptionObject).Message,AppDomain。未处理的异常);
}
}

public class TestForm:Form
{
public TestForm()
{
this.Text =测试应用;
this.ClientSize = new Size(200,60);
this.MinimumSize = this.Size;
this.MaximumSize = this.Size;
this.StartPosition = FormStartPosition.CenterScreen;

按钮btnThrowException = new Button();

btnThrowException.Text =Throw;
btnThrowException.Location = new Point(0,0);
btnThrowException.Size = new Size(200,30);
btnThrowException.Click + =(s,e)=> {throw new Exception(Throw); };

按钮btnThrowExceptionOnOtherThread = new Button();

btnThrowExceptionOnOtherThread.Text =抛出其他线程;
btnThrowExceptionOnOtherThread.Location = new Point(0,30);
btnThrowExceptionOnOtherThread.Size = new Size(200,30);
btnThrowExceptionOnOtherThread.Click + =(s,e)=> new Thread(()=> {throw new Exception(Other thread);})。Start();

this.Controls.Add(btnThrowException);
this.Controls.Add(btnThrowExceptionOnOtherThread);
}
}
}


I have a .NET program with an event handler bound to Application.CurrentDomain.UnhandledException. When running the program with debugging, this event fires when an unhandled exception is thrown. However, when running without debugging, the event does not fire.

What is my problem?

Thanks, Andrew

解决方案

I assume you have not set the correct exception handling mode using Application.SetUnhandledExceptionMode() - just set it to UnhandledExceptionMode.ThrowException.

UPDATE

I just wrote a small test application and found nothing to work unexscpected. Could you try to reproduce your error with this test code?

using System;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;

namespace ConsoleApplication
{
    public static class Program
    {
        static void Main()
        {
            AppDomain.CurrentDomain.UnhandledException += AppDomain_UnhandledException;

            Application.ThreadException += Application_ThreadException;
            Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);

            Application.Run(new TestForm());

            throw new Exception("Main");
        }

        static void Application_ThreadException(Object sender, ThreadExceptionEventArgs e)
        {
            MessageBox.Show(e.Exception.Message, "Application.ThreadException");
        }

        static void AppDomain_UnhandledException(Object sender, UnhandledExceptionEventArgs e)
        {
            MessageBox.Show(((Exception)e.ExceptionObject).Message, "AppDomain.UnhandledException");
        }
    }

    public class TestForm : Form
    {
        public TestForm()
        {
            this.Text = "Test Application";
            this.ClientSize = new Size(200, 60);
            this.MinimumSize = this.Size;
            this.MaximumSize = this.Size;
            this.StartPosition = FormStartPosition.CenterScreen;

            Button btnThrowException = new Button();

            btnThrowException.Text = "Throw";
            btnThrowException.Location = new Point(0, 0);
            btnThrowException.Size = new Size(200, 30);
            btnThrowException.Click += (s, e) => { throw new Exception("Throw"); };

            Button btnThrowExceptionOnOtherThread = new Button();

            btnThrowExceptionOnOtherThread.Text = "Throw on other thread";
            btnThrowExceptionOnOtherThread.Location = new Point(0, 30);
            btnThrowExceptionOnOtherThread.Size = new Size(200, 30);
            btnThrowExceptionOnOtherThread.Click += (s, e) => new Thread(() => { throw new Exception("Other thread"); }).Start();

            this.Controls.Add(btnThrowException);
            this.Controls.Add(btnThrowExceptionOnOtherThread);
        }
    }
}

这篇关于AppDomain.CurrentDomain.UnhandledException无需调试即可触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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