Pregunta

Tengo un programa .NET con un controlador de eventos con destino a Application.CurrentDomain.UnhandledException. Cuando se ejecuta el programa con depuración, este evento se desencadena cuando se produce una excepción no controlada. Sin embargo, cuando se ejecuta sin depurar, el evento no se dispara.

¿Cuál es mi problema?

Gracias, Andrew

¿Fue útil?

Solución

Asumo que no ha ajustado el modo de control de excepciones correcta usando Application.SetUnhandledExceptionMode() - acaba de establecer en UnhandledExceptionMode.ThrowException .

Actualizar

Me acabo de escribir una aplicación de prueba pequeña y no encontré nada para trabajar unexscpected. Podrías tratar de reproducir el error con este código de prueba?

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

Otros consejos

Tal vez la excepción se produce dentro de un hilo separado en su aplicación. Hemos visto el problema de una aplicación simplemente "parar" (medios: un segundo que está ahí, el otro segundo que se ha ido) cuando una excepción no controlada se produce dentro de un hilo. En ese caso, ni siquiera el manejador de excepción no controlada consiguió activa.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top