This project is read-only.
1
Vote

NBug failed to catch exceptions

description

Finally located the root cause. My application uses WindowsFormsApplicationBase to achieve application singleton,

http://www.hanselman.com/blog/TheWeeklySourceCode31SingleInstanceWinFormsAndMicrosoftVisualBasicdll.aspx

Though it is a WinForms application, the unhandled exceptions cannot be caught via
AppDomain.CurrentDomain.UnhandledException += NBug.Handler.UnhandledException;
Application.ThreadException += NBug.Handler.ThreadException;
TaskScheduler.UnobservedTaskException += NBug.Handler.UnobservedTaskException;
To properly catch those exceptions, an extra handler is needed to hook to WindowsFormsApplicationBase.UnhandledException.

Thus, below are the changes I made to NBug in Handler.cs,
public static Microsoft.VisualBasic.ApplicationServices.UnhandledExceptionEventHandler WindowsFormsApplicationBaseUnhandledException
{
    get
    {
        if (Settings.HandleProcessCorruptedStateExceptions)
        {
            return CorruptWindowsFormsApplicationBaseUnhandledExceptionHandler;
        }
        else
        {
            return WindowsFormsApplicationBaseUnhandledExceptionHandler;
        }
    }
}

public static void WindowsFormsApplicationBaseUnhandledExceptionHandler(object sender, Microsoft.VisualBasic.ApplicationServices.UnhandledExceptionEventArgs e)
{
    if (Settings.HandleExceptions)
    {
        Logger.Trace("Starting to handle a System.AppDomain.UnhandledException.");
        var executionFlow = new BugReport().Report(e.Exception, ExceptionThread.Main);
        if (executionFlow == ExecutionFlow.BreakExecution)
        {
            e.ExitApplication = true;
        }
    }
}

[HandleProcessCorruptedStateExceptions]
private static void CorruptWindowsFormsApplicationBaseUnhandledExceptionHandler(object sender, Microsoft.VisualBasic.ApplicationServices.UnhandledExceptionEventArgs e)
{
    WindowsFormsApplicationBaseUnhandledExceptionHandler(sender, e);
}
Of course, NBug project needs to add an reference to Microsoft.VisualBasic.dll. Then applications should hook NBug.Handler.WindowsFormsApplicationBaseUnhandledException to WindowsFormsApplicationBase.UnhandledException.

comments

soygul wrote May 26, 2013 at 11:08 AM

Check that you have .NET 4.0 or above installed properly. You can also try to repair your installation.

wrote May 26, 2013 at 12:30 PM

lextm wrote May 26, 2013 at 12:30 PM

Hi, I have updated the issue and provided my patch.

soygul wrote May 26, 2013 at 1:16 PM

Ok I'll include this fix when I find the time to write some tests and verify the fix. Thanks for your contribution.