AppDomain.CurrentDomain.UnhandledException 未经调试不会触发

发布于 2024-08-02 21:39:38 字数 163 浏览 7 评论 0原文

我有一个 .NET 程序,其事件处理程序绑定到 Application.CurrentDomain.UnhandledException。 当通过调试运行程序时,当抛出未处理的异常时会触发此事件。但是,在不进行调试的情况下运行时,该事件不会触发。

我的问题是什么?

谢谢, 安德鲁

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

歌枕肩 2024-08-09 21:39:38

我假设您没有使用 Application.SetUnhandledExceptionMode() - 只需将其设置为UnhandledExceptionMode.ThrowException

更新

我刚刚编写了一个小型测试应用程序,发现没有任何东西可以正常工作。您可以尝试用此测试代码重现您的错误吗?

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);
        }
    }
}

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);
        }
    }
}
难如初 2024-08-09 21:39:38

也许异常是在应用程序中的单独线程中引发的。我们已经看到了当线程内发生未处理的异常时应用程序只是“停止”的问题(意味着:一秒钟它还在那里,另一秒钟它就消失了)。在这种情况下,甚至未触发的异常处理程序也不会被触发。

Maybe the exception is thrown within a separate thread in your application. We've seen the problem of an application just "stopping" (means: one second it's there, the other second it's gone) when an unhandled exception occurs within a thread. In that case, not even the unhandled exception handler got triggered.

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