未处理的异常错误行和源函数

时间:2012-11-16 02:38:29

标签: vb.net function exception-handling visual-studio-2012 line-numbers

我正在使用VS2012 VB.net。

我可以帮助您创建一些代码来计算异常的错误行以及发生异常的函数。

这是我目前的代码:

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

更新

跟踪代码的代码使用Exception数据类型,其中上面处理unHandled异常的代码的参数为“e As ApplicationServices.UnhandledExceptionEventArgs”。我可以使用此数据类型的跟踪代码吗?我需要将其转换为异常类型吗?或者是不可能的?

2 个答案:

答案 0 :(得分:0)

我没有在Vb.Net掌握,但以前我使用下面的代码,可能它可以帮助你  [ex.StackTrace()]

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

答案 1 :(得分:0)

以下是一些提示。首先是使用PostSharp它的AOP工具包,它将允许您使用属性跟踪所有方法的进入和退出。这将直接引导您进入该功能。

另一招。订阅ThreadExceptionEventHandler确实会导致调试器在未处理的异常中中断!因此,请暂时注释掉MyApplication_UnhandledException并添加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

另一个技巧是不在调试器下运行。由于某种原因,调试器正在屏蔽异常。如果您正常运行应用程序( Ctrl + F5 ),您将获得通常的 Unhandled exception has occurred in your application... Continue/Quit? 对话框。

  

上面处理unHandled异常的代码的参数为“e   作为ApplicationServices.UnhandledExceptionEventArgs“。我可以使用   使用此数据类型跟踪代码?

没有。 您无法轻松地将跟踪代码数据类型与UnhandledExceptionEventArgs一起使用。一个想法可能是创建一个继承自UnhandledExceptionEventArgs的类,但我不知道你如何使用特殊类型调用MyApplication_UnhandledException函数,因为当Exception为{{Unhandled时调用该函数1}}。

相关问题