Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make something that catches all 'unhandled' exceptions in a WinForms application?

Up until now, I just put a try/catch block around the Application.Run in the Program.cs entry point to the program. This catches all exceptions well enough in Debug mode, but when I run the program without the debug mode, exceptions don't get handled anymore. I get the unhandled exception box.

I don't want this to happen. I want all exceptions to be caught when running in non-debug mode. The program has multiple threads and preferably all exceptions from them get caught by the same handler; I want to log exceptions in the DB. Does anyone have any advice in how to do this?

like image 524
Isaac Bolinger Avatar asked Apr 23 '11 06:04

Isaac Bolinger


People also ask

How do you handle unhandled exceptions?

An unhandled exception occurs when the application code does not properly handle exceptions. For example, when you try to open a file on disk, it is a common problem for the file to not exist. The . NET Framework will then throw a FileNotFoundException .

Which method is called in global ASAX to handle all unhandled exceptions?

An ExceptionFilterAttribute is used to collect unhandled exceptions. You can register it as a global filter, and it will function as a global exception handler.

How do I catch all exceptions in Visual Studio?

With a solution open in Visual Studio, use Debug > Windows > Exception Settings to open the Exception Settings window. Provide handlers that respond to the most important exceptions.

Which event is used for unhandled exceptions?

The UnhandledException event is raised for unhandled exceptions thrown in other threads. Starting with Microsoft Visual Studio 2005, the Visual Basic application framework provides another event for unhandled exceptions in the main application thread.


1 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 might also want to not catch exceptions when debugging, as this makes it easier to debug. It is somewhat of a hack, but for that you can wrap the above code around with

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

To prevent catching the exceptions when debugging.

EDIT: An alternate way to check for your application running inside a debugger that feels cleaner than checking a filename.

(see comments by moltenform, Kiquenet and Doug)

if(!System.Diagnostics.Debugger.IsAttached) { ... } 

This avoids the problem of using a different debugger than vshost.exe.

like image 64
Can Gencer Avatar answered Oct 13 '22 08:10

Can Gencer