Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Catch Application Exceptions in a Windows Forms Application

In Windows Forms applications, when an exception is thrown anywhere in the application (on the main thread or during asynchronous calls), you can catch it by registering for the ThreadException event on the Application. In this way you can treat all the exceptions in the same way.

Application.ThreadException += new ThreadExceptionEventHandler(MyCommonExceptionHandlingMethod);

private static void MyCommonExceptionHandlingMethod(object sender, ThreadExceptionEventArgs t)
{
    //Exception handling...
}

I think this is a close as you can get to what you are looking for within a win form application.

http://msdn.microsoft.com/en-us/library/ms157905.aspx

// 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);

Without doing all of these steps you run the risk of having some exceptions get unhandled.


The obvious answer is to put an exception handler at the top of your execution chain.

[STAThread]
static void Main()
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    try
    {
        Application.Run(new YourTopLevelForm());
    }
    catch
    {
        //Some last resort handler unware of the context of the actual exception
    }
}

This will catch any exception that occurs on your main GUI thread. If you also want to globally catch exceptions that occur on all threads you can subscribe to the AppDomain.UnhandledException event and handle there.

Application.ThreadException +=
    new ThreadExceptionEventHandler(MyCommonExceptionHandlingMethod)
private static void MyCommonExceptionHandlingMethod(
                                              object sender,
                                              ThreadExceptionEventArgs t)
{
    //Exception handling...
}

Code copied from Charith J's answer

Now on to the advice ...

These options should only be used as a last resort, say, if you want to suppress accidentally uncaught exceptions from presentation to the user. You should be catching sooner whenever possible, when you know somthing about the context of the exception. Even better, you may be able to do something about the problem.

Structured exception handling might seem like an unecessary overhead which you can work around with a catch all but, it exists because this is not the case. What is more, this work should be done as the code is written, when the developer has the logic fresh in thier mind. Do not be lazy and leave this work for later or for some more profressional developer to pick up.

Apologies if you already know and do this.


You can subscribe to AppDomain.UnhandledException Event