17

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

AndrewL
  • 3,126
  • 5
  • 31
  • 33

2 Answers2

17

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);
        }
    }
}
Daniel Brückner
  • 59,031
  • 16
  • 99
  • 143
  • Setting the correct mode didn't help in my case. This is what I had to try. https://stackoverflow.com/a/70889777/1520030 – Umar Hassan Jan 28 '22 at 06:45
0

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.

Thorsten Dittmar
  • 55,956
  • 8
  • 91
  • 139
  • I'm throwing a test exception in the normal UI thread, and the event doesn't fire. Throwing an exception in a thread separate than that does cause the event to fire. – AndrewL Aug 19 '09 at 14:43
  • 3
    The `AppDomain.UnhandledException` event is there precisely to allow you to get notified when you have an unhandled exception on another thread. If a thread has an unhandled exception the process will terminate exactly as you describe. – Martin Liversage Sep 12 '11 at 11:37