Loupe - Log - Monitor - Resolve
Loupe / Developer's Guide / For .NET Framework / For WinForms / Exception Handling / Developer's Guide for WinForms - Exception Handling - Creating your own Custom Exception Handling
In This Topic
    Developer's Guide for WinForms - Exception Handling - Creating your own Custom Exception Handling
    In This Topic

    Performing your Own Unhandled Exception Handling

    By default, the Loupe Error Manager will trap Application Exceptions within any WinForms application and record unhandled exceptions before the application exits.  If you want to have your own total control over exception handling you will want to disable this behavior by setting the catchUnhandledExceptions and catchApplicationExceptions options of the Listener configuration to false.

    Next, you’ll want to add some code to the main function of your program to register for the following events:

    You should do this at the very beginning of your program.  In particular, for WinForm applications you should do this before starting the first form.

    Whatever else you do in your event handler, don’t forget to log each unhandled exception to the Loupe Agent so that this information is included in your logs and available to the Loupe Desktop.  The best way to do this is to directly call the Agent API which has two functions specifically for this scenario:

    Logging an Unhandled Exception to Loupe
    Copy Code
    using Gibraltar.Agent;
    
    public static void Application_ThreadException(object sender, ThreadExceptionEventArgs ex)
    {
           //this option records the exception but does not display any user interface. 
           Log.RecordException(ex, "Exceptions", true);
           //this option records the exception and displays a user interface, optionally waiting for the user
           //to decide to continue or exit before returning.
           Log.ReportException(ex, "Exceptions", true, true);
    }
    

    The RecordException method is primarily intended for situations where you want to display your own user interface but still want to record the information in the log.  The advantage of the RecordException method over other logging methods (like Log.TraceError or Log.Error) is that the log statement will be attributed to the source code location where it was thrown, making it much easier to understand where the error was generated.

    This example is taken from the code samples in the Loupe SDK folder.

    Exception Handling in WinForms Applications

    To register your own handler instead of the Agent's, make sure to subscribe to the Application.ThreadException event after logging a message to Loupe so the Agent sees the thread and before calling Application.Run().

    Exception Notification vs. Handling

    The AppDomain.UnhandledException event is not an exception handler but rather an event that notifies you that an exception has occurred.  Once the event handler returns, the .NET runtime will end the process, potentially displaying a crash dialog to the end user.

    Submitting Session Data on an Exception

    You can use the Packager Dialog built into the Agent to have all of the session data up through and including the exception packaged and emailed to you for support.  For full details on how to use the Packager Dialog, see Getting Started - Packager Dialog.

    BackgroundWorker Exception Handling

    The BackgroundWorker class provides its own model for assisting with exception management.  For more information an example of how to work with it, see Developer's Guide - Exception Handling with the BackgroundWorker.

    See Also