Catching an unhandled exception, still showing the "stopped working" dialog

I read in this post to avoid the “stopped working” dialog, I need to catch an unhandled exception from AppDomain.

public Form1() { ///Code AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException; ///more code } void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) { var excep = e.ExceptionObject; //Writing the exception to log file with the stack flow Logger.Logger.LogException("UNHANDLED EXCEPTION:"+Environment.NewLine +excep.ToString(), this); //Terminate the logger (manual event waiting for file write to finish) Logger.Logger.Terminate(); Environment.Exit(1); } 

But when I inhale the exception. I see that this is recorded in the log, but the application displays a "stopped working" dialog. Could this be caused by the Logger.Terminate line? (again - the terminate command waits until all the logs are written to the log file)

+5
source share
1 answer

Ideally, you want to avoid the “stop working with the program” dialog, because, well, you want to avoid the “stop working with the program” condition for starters. If not, if you just want to register an error and let the program finish gracefully, the program really should finish gracefully.

If you exit the program using Environment.Exit(1) , you pretty much tell the operating system, “hey, I tried!”, Which is the opposite of a graceful finish. Try to exit with code 0 and see if it matters.

0
source

Source: https://habr.com/ru/post/1236793/


All Articles