AppDomain.CurrentDomain.UnhandledException does not fire without debugging

I have a .NET program with an event handler associated with Application.CurrentDomain.UnhandledException. When a program with debugging starts, this event is triggered when an unhandled exception is thrown. However, when starting without debugging, the event does not fire.

What is my problem?

Thanks Andrew

+12
exception unhandled-exception
Aug 19 '09 at 14:23
source share
2 answers

I assume that you did 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 did not find anything that could be used not in the know. 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); } } } 
+14
Aug 19 '09 at 14:54
source share

Perhaps the exception is thrown in a separate thread in your application. We saw that the application problem simply โ€œstopsโ€ (meaning: for one second it is there, and the other second is not) when an unhandled exception occurs in the thread. In this case, even the processed exception handler did not start.

0
Aug 19 '09 at 2:37 a.m.
source share



All Articles