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.
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
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();
}
}
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.