Not able to Catch unhandled exception

Santosh P 1 Reputation point
2021-08-06T06:20:04.883+00:00

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
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,918 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Karen Payne MVP 35,466 Reputation points
    2021-08-06T15:28:55.537+00:00

    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.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.