question

SantoshP-0338 avatar image
0 Votes"
SantoshP-0338 asked DanielZhang-MSFT commented

Not able to Catch unhandled exception

I am using basic windows application. In my application I want to log/display the details of any unhandled exception occurred . So I used following code to handle the exceptions.
But following code is executing when run through the Visual studio, But the same is not executing when I run through the exe.
Please help me how I can execute the following through exe also.

Thanks
Santosh

public static class Program
{
public static MainForm myMainForm = null;


     // Starts the application. 
     
     /// <summary>
     /// The main entry point for the application.
     /// </summary>
     [STAThread]
     public static void Main()
     {
         //Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
         // FIXE WINDOWS BUGS : 
         // => affecte les images lists            
         // ces deux lignes doivent être les première du programme
         // si pas de VisualStyles, desactiver les deux
         Application.EnableVisualStyles();
         Application.DoEvents();
         Application.ThreadException += new ThreadExceptionEventHandler(App_UiThreadException);
         // Set the unhandled exception mode to force all Windows Forms errors to go through // our handler.
         //Application.SetUnhandledExceptionMode(UnhandledExceptionMode.ThrowException);
         // Set the unhandled exception mode to force all Windows Forms errors to go through
         // our handler.
         AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(UnhandledException);
      
             myMainForm = new MainForm();
             Application.Run(myMainForm);
             myMainForm = null;
              
     }
      
     /// <summary>
     /// Handles the UnhandledException event of the current AppDomain.
     /// </summary>
     /// <param name="sender">The source of the event.</param>
     /// <param name="args">The instance containing the event data.</param>
     private static void UnhandledException(object sender, UnhandledExceptionEventArgs e)
     {
         //// Force the application to save the current configuration.
         UserConfiguration.GetTheConfiguration().Save();
            
            Exception ex = e.ExceptionObject as Exception;
             if (ex != null)
             {
                  MessageBox.Show(ex.ToString());
                 //log the exception details in exception log file.
             }
            

         //Application.Exit();
         //#endif
     }

     /// <summary>
     /// Handles the thread exception event.
     /// </summary>
     /// <param name="sender">The source of the event.</param>
     /// <param name="args">The instance containing the event data.</param>
     public static void App_UiThreadException(object sender, ThreadExceptionEventArgs args)
     {

         //// Force the application to save the current configuration.
         UserConfiguration.GetTheConfiguration().Save();

            
             Exception ex = args.Exception as Exception;
             if (ex != null)
             {
                 MessageBox.Show(ex.ToString());
                 //log the exception details in exception log file.
             }
            
         Application.Exit();
     }
 }
windows-forms
· 8
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.


How did you discover the issue?

By the way, is 'Application.DoEvents()' necessary?


0 Votes 0 ·

Hello Viorel-1,

Thank you for reply.

I discovered this issue during validation of my application. The unhandled exceptions are not logging into exception log file.

I even tried by removing Application.DoEvents(). It is not working.

Thanks
Santosh P

0 Votes 0 ·

Which exceptions were not logged?


0 Votes 0 ·
Show more comments

Hi @SantoshP-0338.
You can try to use the Application.SetUnhandledExceptionMode method to change the mode to UnhandledExceptionMode.CatchException before hooking the ThreadException event handler.

 Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);

Best Regards,
Daniel Zhang



0 Votes 0 ·

Hello Daniel,

Thanks for reply.

I tried based on your suggestion but it is giving following exception.

'Thread exception mode cannot be changed once any Controls are created on the thread.'

Regards
Santosh

0 Votes 0 ·

Hi @SantoshP-0338,
Have you tried putting a try / catch block in your main method?
Please refer to the following threads.
Preventing Unhandled Exception Dialog Appearing
What causes Winforms to silently discard unhandled exceptions (with no try/Catches)?
Best Regards,
Daniel Zhang


0 Votes 0 ·

1 Answer

karenpayneoregon avatar image
0 Votes"
karenpayneoregon answered

You might try my NuGet package for unhandled exceptions if using .NET Core 5 Framework, instructions.

And try with and without UserConfiguration.GetTheConfiguration().Save(); or wrap it in a try/catch.


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.