2016-11-14 108 views
0

我当前安装的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 

这不允许测试处理程序,但它会执行处理程序调用的代码。看着一些老的意见自从五+年前想通了这一点... :(

+0

我'bntTest_Click'代码是一个MDI父窗体中子窗体上。 – rheitzman

+0

后看到你在ApplicationEvents中的编辑,这与我添加处理程序的方式不一样。我尝试了你的代码并确认它不起作用。尝试我尽可能的方式。 – djv

+0

我尝试从MDI容器中引发测试异常(表单级别)并且它也失败了,你的方法是以独立的形式工作,但是我需要在应用程序级别捕获未处理的事件,因为我有独立和MDI-Child形式的混合。 – rheitzman

回答

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 
+0

我有处理程序,当exe在IDE外运行时工作。我试图从IDE调试处理程序。当我抛出一个未处理的异常时,就像在帖子中一样,IDE停止并且不会调用处理程序。 – rheitzman

+0

奇怪的是,我的IDE调用处理程序*,然后*暂停异常。运行Win7x64 VS2012 v4.6,我的经验似乎jive与http://stackoverflow.com/questions/406385/handling-unhandled-exceptions-problem – djv

+0

我认为不同可能是C#vs VB.net和VB应用程序事件功能的Visual Studio。可能是2012年和2015年,因为我认为我可以一次调试这个事件。 – rheitzman