How to catch an unhandled exception in C #?

I'm trying to catch any exception thrown in my program, I use this code in the main class of the program

static class Program { /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException); Application.Run(new Form1()); } public static void CurrentDomain_UnhandledException(Object sender, UnhandledExceptionEventArgs e) { MessageBox.Show((e.ExceptionObject as Exception).Message, "Unhandled domain Exception"); Application.Exit(); } 

now im my form form1, I'm trying to create a simple exception: dividing by zero, without a catch try block, the code in the main module really catches the exception, but I still have the Visual Studio MS dialog. the application does not exit. Of course, in real situations, I willl log / mail error. but I would like to understand why execution continues after I caught my exception? thanks

+4
source share
3 answers

Capturing all exceptions from the main UI thread worked for me:

 static class Program { /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); // Add handler for UI thread exceptions Application.ThreadException += new ThreadExceptionEventHandler(UIThreadException); // Force all WinForms errors to go through handler Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException); // This handler is for catching non-UI thread exceptions AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException); Application.Run(new Form1()); } private static void CurrentDomain_UnhandledException(Object sender, UnhandledExceptionEventArgs e) { try { Exception ex = (Exception)e.ExceptionObject; MessageBox.Show("Unhadled domain exception:\n\n" + ex.Message); } catch (Exception exc) { try { MessageBox.Show("Fatal exception happend inside UnhadledExceptionHandler: \n\n" + exc.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Stop); } finally { Application.Exit(); } } // It should terminate our main thread so Application.Exit() is unnecessary here } private static void UIThreadException(object sender, ThreadExceptionEventArgs t) { try { MessageBox.Show("Unhandled exception catched.\n Application is going to close now."); } catch { try { MessageBox.Show("Fatal exception happend inside UIThreadException handler", "Fatal Windows Forms Error", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Stop); } finally { Application.Exit(); } } // Here we can decide if we want to end our application or do something else Application.Exit(); } } 
+9
source

You do not need to use Application.Exit or Environment.Exit, it is just the behavior of the visual studio that it continues to catch the string of unhandled exceptions, but in fact your application will crash (exit) if the flag isTerminating and this is its correct use. I would recommend not using any user interface for this or any other resource-intensive process in this case.

the purpose of this event is to get a log of why this happened this way, this does not mean that your application looks good while it crashes, or apologizing for it. but you will have the opportunity to do it higher the next time the user logs in.

Therefore, even if you plan to send an error log, the best way is to write the file and send the file by e-mail the next time you start the application.

I usually use log4net and use these events as follows.

 [STAThread] static void Main() { log.Info("Starting deployer application"); Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException); AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException); Application.ApplicationExit += new EventHandler(Application_ApplicationExit); Application.Run(ConnectionForm.GetInstance); } static void Application_ApplicationExit(object sender, EventArgs e) { log.Info("Application Closed"); } static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) { log.Error(string.Format("*** UNHANDLED APPDOMAIN EXCEPTION ({0}) *****", e.IsTerminating ? "Terminating" : "Non-Terminating"), e.ExceptionObject as Exception); } static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e) { log.Error("*** UNHANDLED THREAD EXCEPTION *****", e.Exception); } 

If you have never used log4net before this may be useful .

+1
source
 Use Environment.Exit() 

instead

 Application.Exit() 
0
source

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


All Articles