如何调试unhandle异常处理

时间:2016-11-14 20:41:51

标签: vb.net visual-studio visual-studio-2015

我当前安装的Visual Studio 2015将不允许我在从IDE运行代码时抛出未处理的异常。我想练习我未处理的异常代码但是我的代码:

Private Sub btnTest_Click(sender As System.Object, e As System.EventArgs) Handles btnTest.Click
    Throw New System.Exception("An unhandled test exception has occurred.")
End Sub

仅在正常运行时期间工作,而不是在IDE中执行代码时。

如何在IDE中调试未处理的异常代码?

我查看了调试,Windows,异常设置,但我没有办法做我想做的事情。是否有另一个更全局的设置允许未处理异常而IDE没有捕获异常?

我正在使用ApplicationsEvents.vb挂钩事件:

Namespace My

' The following events are available for MyApplication:
' 
' Startup: Raised when the application starts, before the startup form is created.
' Shutdown: Raised after all application forms are closed.  This event is not raised if the application terminates abnormally.
' UnhandledException: Raised if the application encounters an unhandled exception.
' StartupNextInstance: Raised when launching a single-instance application and the application is already active. 
' NetworkAvailabilityChanged: Raised when the network connection is connected or disconnected.
Partial Friend Class MyApplication
    Private Sub MyApplication_UnhandledException(sender As Object, e As Microsoft.VisualBasic.ApplicationServices.UnhandledExceptionEventArgs) Handles Me.UnhandledException
        ExpLog.LogUnhandledException(e, sender)
        e.ExitApplication = Not ExpLog.InformUser
    End Sub
End Class
End Namespace

解决方案是创建一个测试存根,它运行处理程序调用的代码:

Private Sub Button6_Click(sender As System.Object, e As System.EventArgs) Handles btnMisc_Throw.Click

    If ExpLog.InformUser() Then
        MsgBox("Continue")
    Else
        MsgBox("End Program")
    End If

    ExpLog.LogMsgBox(New System.Exception("test unhandled exception"), "test LogMsgBox()",,, "programmer note")

End Sub

这不允许测试处理程序,但它确实运行了处理程序调用的代码。看一些旧的评论,我在五年前发现了这个... :(

1 个答案:

答案 0 :(得分:0)

AppDomain.CurrentDomain.UnhandledException

添加处理程序
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    AddHandler AppDomain.CurrentDomain.UnhandledException, AddressOf UnhandledExceptionHandler
End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Throw New System.Exception("An unhandled test exception has occurred.")
End Sub

Public Shared Sub UnhandledExceptionHandler(ByVal sender As Object, ByVal args As UnhandledExceptionEventArgs)
    MessageBox.Show(CType(args.ExceptionObject, Exception).Message)
End Sub