我从一个具有FormClosing处理程序的表单继承我的类(它可以覆盖,所以我可以在调用base方法之前覆盖它并在那里执行MsgBox(“Ha”))。在我的Shared Sub New()中,我有:
' Add the event handler for handling UI thread exceptions to the event.
AddHandler Application.ThreadException, AddressOf Application_ThreadException
' Add the event handler for handling non-UI thread exceptions to the event.
AddHandler AppDomain.CurrentDomain.UnhandledException, AddressOf CurrentDomain_UnhandledException
'For Console applications you should use the System.AppDomain.UnhandledException event
AddHandler Thread.GetDomain().UnhandledException, AddressOf CurrentDomain_UnhandledException
如果我在try / catch之外的表单中抛出异常,我注意到两种不同的行为:
我很沮丧为什么#2中没有调用Unhandled Exception处理程序。理想情况下,我希望(在两种情况下)都要调用Unhandled Exception,然后调用FormClosing事件处理程序。我错过了什么?
(某些示例代码) - 这演示了如果在VS中运行调试器(只需按F5)或在没有调试器的情况下运行(只需按CTRL-F5),将抛出不同的异常。这不能完美地重现问题,但也许我的问题与基类的处理不同的ThreadException有关。
Form1(在项目中设置为启动)。
Imports System.Threading
Public Class Form1
Inherits Form2
Shared Sub New()
' Add the event handler for handling UI thread exceptions to the event.
AddHandler Application.ThreadException, AddressOf Application_ThreadException
' Add the event handler for handling non-UI thread exceptions to the event.
AddHandler AppDomain.CurrentDomain.UnhandledException, AddressOf CurrentDomain_UnhandledException
End Sub
Private Shared Sub CurrentDomain_UnhandledException(ByVal sender As Object, ByVal e As UnhandledExceptionEventArgs)
MsgBox("UnhandledException caught")
End Sub
Private Shared Sub Application_ThreadException(ByVal sender As Object, ByVal e As ThreadExceptionEventArgs)
MsgBox("ThreadException caught")
End Sub
Protected Overrides Sub Login()
Throw New Exception("Ha!")
MyBase.Login()
End Sub
Protected Overrides Sub Form2_FormClosing(sender As System.Object, e As System.Windows.Forms.FormClosingEventArgs)
MsgBox("Form1Closing")
MyBase.Form2_FormClosing(sender, e)
End Sub
End Class
窗体2:
Public Class Form2
Protected Overridable Sub Form2_FormClosing(sender As System.Object, e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
MsgBox("Form2Closing")
End Sub
Private Sub Form2_Shown(sender As System.Object, e As System.EventArgs) Handles MyBase.Shown
Login()
End Sub
Protected Overridable Sub Login()
MsgBox("Form2 LoggingIn")
End Sub
End Class
答案 0 :(得分:0)
我很抱歉没有使用评论部分,也许它应该在那里(没有足够的代表)。我以为我会把我的2c研究投入:
一切似乎都按预期工作。