0

I am using VS2012 VB.net.

Can I please have some help in creating some code to calculate the error line of an exception and also the function that the exception occurred in.

Here is my current code:

Partial Friend Class MyApplication

    Public exceptionListOfExceptionsToNotPauseOn As New List(Of ApplicationServices.UnhandledExceptionEventArgs)

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

        Dim msgboxResult As MsgBoxResult
        Dim booleanExceptionFoundInList As Boolean = False

        'Dim trace As System.Diagnostics.StackTrace = New System.Diagnostics.StackTrace(ex, True)
        'Dim exceptionLineNumber = trace.GetFrame(0).GetFileLineNumber()

        For x = 0 To exceptionListOfExceptionsToNotPauseOn.Count - 1
            If exceptionListOfExceptionsToNotPauseOn(x).Exception.Message = e.Exception.Message Then
                booleanExceptionFoundInList = True
            End If
        Next

        If Not booleanExceptionFoundInList Then
            msgboxResult = MessageBox.Show("An exception error has occured." & vbCrLf & "Error message: " & e.Exception.Message & vbCrLf & "Do you wish to pause on this exception again?", "Exception", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question)

            If msgboxResult = Microsoft.VisualBasic.MsgBoxResult.No Then
                exceptionListOfExceptionsToNotPauseOn.Add(e)
            End If
        End If

        e.ExitApplication = False

    End Sub
End Class

UPDATE

The code for the trace code uses an Exception data type where as the code for handling unHandled exceptions above has a parameter of "e As ApplicationServices.UnhandledExceptionEventArgs". Can I use the trace code with this data type? Do I need to cast it to an exception type? Or is it not possible?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
user1690531
  • 231
  • 1
  • 6
  • 17
  • How did you go with tracking down this exception? Hoping no news is good news since its still a first chance exception the debugger CAN catch. Maybe go back to source control and see what code changes were made to the specific function causing the problem the time the problem started to occur. – Jeremy Thompson Nov 24 '12 at 05:08

2 Answers2

0

I am not mastering in Vb.Net but previously i used below code, may be it can help you [ex.StackTrace()]

 Try
  'Your Code goes here
 Catch ex As Exception
  MsgBox(ex.StackTrace())
 End Try
0

Here are a couple of tips. First is to use PostSharp its a AOP kit that will let you trace all methods entry and exit using Attributes. This would direct you to the function straight away.

Another trick. Subscribing to the ThreadExceptionEventHandler actually does cause the debugger to break on unhandled exceptions! Hence temporarily comment out your MyApplication_UnhandledException and add a ThreadExceptionEventHandler

<STAThread> _
Public Shared Sub Main(args As String())
    Try
        'your program entry point
        Application.ThreadException += New ThreadExceptionEventHandler(Application_ThreadException)
            'manage also these exceptions
    Catch ex As Exception
    End Try
End Sub

Private Sub Application_ThreadException(sender As Object, e As ThreadExceptionEventArgs)
    ProcessException(e.Exception)
End Sub

Another trick is is not to run under the debugger. The debugger is masking the exception for some reason. If you run your app normally (Ctrl+F5), you'll get the usual Unhandled exception has occurred in your application... Continue/Quit? dialog.

The code for handling unHandled exceptions above has a parameter of "e As ApplicationServices.UnhandledExceptionEventArgs". Can I use the trace code with this data type?

No. You cannot easily use your trace code datatype with the UnhandledExceptionEventArgs. One idea might be to make a class that inherits from UnhandledExceptionEventArgs but I don't know how you'd call the MyApplication_UnhandledException function with the special type, because that function is invoked when an Exception is Unhandled.

Be Brave Be Like Ukraine
  • 7,596
  • 3
  • 42
  • 66
Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321