How can I do something that catches all “unhandled” exceptions in a WinForms application?

So far, I just put a try / catch block around Application.Run at the entry point of Program.cs into the program. This complicates all exceptions in debug mode quite a bit, but when I run the program without debug mode, the exceptions are no longer handled. I get an unhandled exception field.

I do not want this to happen. I want all exceptions to be caught when working in non-debug mode. A program has several threads, and preferably all exceptions to them fall into the same handler; I want to register exceptions in the database. Does anyone have any advice on how to do this?

+72
debugging c # winforms unhandled-exception
Apr 23 2018-11-11T00:
source share
4 answers

Take a look at the example from the ThreadException documentation :

 public static void Main(string[] args) { // Add the event handler for handling UI thread exceptions to the event. Application.ThreadException += new ThreadExceptionEventHandler(ErrorHandlerForm.Form1_UIThreadException); // Set the unhandled exception mode to force all Windows Forms errors // to go through our handler. Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException); // Add the event handler for handling non-UI thread exceptions to the event. AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException); } 

You can also ignore exceptions when debugging, as this makes debugging easier. It's a bit of a hack, but for that you can wrap the code above with

  if (!AppDomain.CurrentDomain.FriendlyName.EndsWith("vshost.exe")) { ... } 

To avoid exception exceptions during debugging.

+96
Apr 23 2018-11-11T00:
source share

In NET 4, some exceptions are no longer caught by default; they are typically exceptions indicating a (possibly fatal) damaged state of the executable, for example, AccessViolationException.

Try using the [HandleProcessCorruptedStateExceptions] tag before your main method, for example

 using System.Runtime.ExceptionServices.HandleProcessCorruptedStateExceptions [HandleProcessCorruptedStateExceptions] public static int Main() { try { // Catch any exceptions leaking out of the program CallMainProgramLoop(); } catch (Exception e) // We could be catching anything here { System.Console.WriteLine(e.Message); return 1; } return 0; } 
+25
Apr 23 '11 at 8:26
source share

A good example can be found at http://www.csharp-examples.net/catching-unhandled-exceptions/. Basically, change your main:

 static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.ThreadException += new ThreadExceptionEventHandler(Application_ThreadException); Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException); AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException); Application.Run(new Form1()); } static void Application_ThreadException(object sender, ThreadExceptionEventArgs e) { MessageBox.Show(e.Exception.Message, "Unhandled Thread Exception"); } static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) { MessageBox.Show((e.ExceptionObject as Exception).Message, "Unhandled UI Exception"); } 
+10
Nov 12 '15 at 11:42
source share

You can use NBug for this. With minimal setup:

 NBug.Settings.Destination1 = "Type=Mail;From=me@mycompany.com;To=bugtracker@mycompany.com;SmtpServer=smtp.mycompany.com;"; AppDomain.CurrentDomain.UnhandledException += NBug.Handler.UnhandledException; Application.ThreadException += NBug.Handler.ThreadException; 

You can start collecting information about all the unhandled errors in your application, even when they are deployed to clients. If you do not want to use a third-party library, you must connect to the following events:

 // These two should come before enabling visual styles or running the application AppDomain.CurrentDomain.UnhandledException += ... Application.ThreadException += ... ... Application.Run(new Form1()); 
+8
Apr 23 2018-11-11T00:
source share



All Articles