我试图弄清楚为什么会这样。在我的vb.net应用程序中,我在ApplicationEvents.vb中设置了一个全局处理程序,我认为它只会拾取任何未处理的异常,尽管它会拾取应用程序中发生的所有异常,无论是否使用try catch块进行处理。这是我在applicationevents中的代码
Private Sub MyApplication_UnhandledException(ByVal _
sender As Object, ByVal e As _
Microsoft.VisualBasic.ApplicationServices.UnhandledExceptionEventArgs) _
Handles Me.UnhandledException
e.ExitApplication = _
MessageBox.Show(e.Exception.Message & _
vbCrLf & "The application has encountered a bug, would you like to Continue?", "An Error has occured.", _
MessageBoxButtons.YesNo, _
MessageBoxIcon.Question) _
= DialogResult.No
End Sub
在我的应用程序的其余部分中,我设置了普通的try catch块,如下所示:
Try
Catch ex as exception
End Try
谁能告诉我为什么会这样吗?
最佳答案
我创建了一个VB项目。 ApplicationEvents.vb看起来像这样:
Partial Friend Class MyApplication
Private Sub MyApplication_UnhandledException(sender As Object, e As Microsoft.VisualBasic.ApplicationServices.UnhandledExceptionEventArgs) Handles Me.UnhandledException
MsgBox("Unhandled exception")
End Sub
End Class
表单的代码为:
Public Class Form1
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim a As Integer
Dim b As Integer
Try
a = a / b
Catch ex As Exception
MsgBox("Handled exception")
End Try
End Sub
End Class
重要的是,如果您没有捕获表格中的错误,则可能不会触发常规错误处理程序(取决于Visual Studio上的设置,我不记得是哪个)。可以肯定的是,无需调试即可执行项目(CTRL + F5而不是F5)
关于.net - 用于未处理异常的全局错误捕获器正在拾取所有异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13399602/