Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net AppDomain.CurrentDomain.UnhandledException未经调试不会触发_.net_Exception_Unhandled Exception - Fatal编程技术网

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

.net AppDomain.CurrentDomain.UnhandledException未经调试不会触发,.net,exception,unhandled-exception,.net,Exception,Unhandled Exception,我有一个.NET程序,其事件处理程序绑定到Application.CurrentDomain.UnhandledException。 在运行带有调试的程序时,当引发未处理的异常时,将触发此事件。但是,在未调试的情况下运行时,不会触发事件 我有什么问题 谢谢, Andrew可能在应用程序中的单独线程中引发异常。我们已经看到当线程中发生未处理的异常时,应用程序只是“停止”(意思是:一秒钟它在那里,另一秒钟它消失了)的问题。在这种情况下,甚至没有触发未处理的异常处理程序。我假设您没有使用-将其设置为正

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

我有什么问题

谢谢,
Andrew

可能在应用程序中的单独线程中引发异常。我们已经看到当线程中发生未处理的异常时,应用程序只是“停止”(意思是:一秒钟它在那里,另一秒钟它消失了)的问题。在这种情况下,甚至没有触发未处理的异常处理程序。

我假设您没有使用-将其设置为正确的异常处理模式

更新

我只是写了一个小的测试应用程序,并没有发现任何意外的工作。你能用这个测试代码重现你的错误吗

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

我在普通UI线程中抛出了一个测试异常,事件没有触发。在单独的线程中引发异常会引发事件。
AppDomain.UnhandledException
事件正好允许您在另一个线程上出现未处理的异常时收到通知。如果线程有未处理的异常,进程将完全按照您所描述的那样终止。