Search code examples
winformsexceptionhandlermessagebox

MessageBox not showing when "Unhandled Exception" triggers in winforms


so I was trying to add "AppDomain.CurrentDomain.UnhandledException" handler to my application and it worked OK if I log the error to a text file. but when I try to use a MessageBox it never pops. is it another bug in .Net? any ideas?

here is my code sample:

   AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);

   Application.EnableVisualStyles();
   Application.SetCompatibleTextRenderingDefault(false);
   Application.Run(new Form1());

and here is my handler method:

static void CurrentDomain_UnhandledException (object sender, UnhandledExceptionEventArgs e)
{
    try
    {
       Exception ex = (Exception)e.ExceptionObject;

       MessageBox.Show("Whoops! Please contact the developers with "
                   + "the following information:\r\n\r\n" + ex.Message + ex.StackTrace,
                   "Fatal Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);

    }
    finally
    {
       Application.Exit();
    }
}

EDIT: I've tried all of the possible options but I still can't see the MessageBox. Now the problem is when I run it from Visual C# (debug mode) it perfectly shows the box. but when I run the application directly from the debug/release folders it doesn't shows the MessageBox and the Application keeps running like there is no error is happening...


Solution

  • this example works for me in debug mode and release mode with vs2010 or not:

    using System;
    using System.Windows.Forms;
    
    namespace WinFormsStackOverflowSpielWiese
    {
      internal static class Program
      {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        private static void Main() {
          System.Windows.Forms.Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
          System.Windows.Forms.Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
          AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
    
          Application.EnableVisualStyles();
          Application.SetCompatibleTextRenderingDefault(false);
          Application.Run(new Form1());
        }
    
        private static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e) {
          try {
            var exception = e.Exception != null ? e.Exception.Message + e.Exception.StackTrace : string.Empty;
            MessageBox.Show("Whoops! Please contact the developers with the following information:\r\n\r\n" + exception,
                            "Fatal Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);
          }
          finally {
            Application.Exit();
          }
        }
    
        private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) {
          try {
            var exception = e.ExceptionObject is Exception ? ((Exception)e.ExceptionObject).Message + ((Exception)e.ExceptionObject).StackTrace : string.Empty;
            MessageBox.Show("Whoops! Please contact the developers with the following information:\r\n\r\n" + exception,
                            "Fatal Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);
          }
          finally {
            Application.Exit();
          }
        }
      }
    }
    

    code for the form

    EDIT now with a timer, with the same result....

    using System.Windows.Forms;
    
    namespace WinFormsStackOverflowSpielWiese
    {
      public partial class Form1 : Form
      {
        private System.Threading.Timer myTimer;
    
        public Form1() {
          this.InitializeComponent();
    
          this.myTimer = new System.Threading.Timer(state =>
                                                      {
                                                        var i = 0;
                                                        var s = 100 / i;
                                                      }
                                                    , null, 5000, 5000);
        }
      }
    }