views:

211

answers:

2

I have a Windows Form application that has a global error handler to display unexpected errors.

Namespace My

    Class MyApplication

        Delegate Sub ProcessParametersDelegate(ByVal sender As Object, ByVal args() As String)

        Private Sub MyApplication_UnhandledException(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.ApplicationServices.UnhandledExceptionEventArgs) Handles Me.UnhandledException

            Dim ErrorText As String = e.Exception.Message & ", " & e.Exception.StackTrace

            Try                    
                MessageBox.Show("Global Error Handler: An unhandled error occurred. It will be copied to your clipboard upon OKing this message: " & ErrorText)
                Clipboard.SetText(ErrorText)
            Catch ex As Exception

            End Try

        End Sub

    End Class

End Namespace

In VS2008, I have my Debug - > Exceptions options set to the defaults, which are to raise an error on all unhandled exceptions.

I have a few issues:

1) for the same deployed code, I SOMETIMES get line number in the displayed StackTrace error message and sometimes do not, even when the error message includes source code that I have written rather than a referenced binary. The project is compiled with a DEBUG configuration.

2) The application strangely minimizes to the tray when the error occurs (I thin it is unlikely that anyone can diagnose this issue w/o my more code posted, but I'll mention it anyways)

3) When I try to intentionally raise an error by, for example, dividing by zero hoping to test by global error handler, I get a dialog error message from the interactive debugger rather than jumping into my global error handler (which i want to debug because there is more to it than I posted.) Do you have any idea how to triggerand force teh execution of the global event handler?

A: 

Regarding pt 3:

If your only relevant handler for an exception is the global exception handler, then the debugger treats the exception as "unhandled." Essentially, you cannot step through your global event handler as you are trying to do.

Velika
1 outa three is good enough to claim the prize.
Velika
Hmm...I see this in my immediate window: Warning: Cannot debug script code. Incorrect function
Velika
+1  A: 

1)

If you are missing part of your stack trace, it is likely because of how you are ignoring inner exceptions.

Instead of:

Dim ErrorText As String = e.Exception.Message & ", " & e.Exception.StackTrace

Use:

Dim ErrorText As String = e.Exception.ToString()

Also, make sure your .pdb file gets deployed to the same location as your .exe file.

2) Not sure.

3)

To step through MyApplication_UnhandledException in the debugger:

Debug -> Start Without Debugging

Debug -> Attach to Process

Find your app name in the "Attach to Process" window and Attach to it. Your breakpoint will now be hit.

I like to use this to force an exception, but there are many other options:

MessageBox.Show(Nothing.ToString())
Michael Maddox
Your answer sounded good, but it didn't work. I followed the instructions and the break point was never hit. The exception handler, in tis case, displayed line number information using the StackTrace method as well as your advised .ToString method, although I believe this suggestion is probably a good one.
Velika
2 min video of me following your instructions
Velika
Forgot to post the URL: http://screencast.com/t/ZPv00EOrCnTl
Velika
Once you attach the debugger, can you hit any breakpoint anywhere? It looked like you had a breakpoint before the divide by zero line and that breakpoint didn't get hit either. It worked fine for me in a tiny project I created while posting my answer. Your screencast looked fine, other than I'm not convinced the debugger was truly attached, but not for any solid reason. I would say sprinkle some more breakpoints around and keep trying. Sometimes the debugger doesn't like breakpoints on lines where variables are declared. Good luck.
Michael Maddox