WinForms Global Exception Handling?

If #26 is an exception then you can use AppDomain.CurrentDomain.UnhandledException event. If it’s just a return value, then I don’t see any chance to handle that globally.

public static void Main()
{
    AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(MyHandler);

    // start main thread here
}

static void MyHandler(object sender, UnhandledExceptionEventArgs args) 
{
    Exception e = (Exception) args.ExceptionObject;
    Console.WriteLine("MyHandler caught : " + e.Message);
}

Leave a Comment