我从一个具有FormClosing处理程序的表单继承我的类(它是可重写的,所以在调用基方法之前,我可以重写它并在那里执行MsgBox("Ha") )。在我的共享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中调用未处理的异常处理程序,理想情况下,我希望(在这两种情况下)调用未处理的异常,然后调用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 ClassForm2:
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发布于 2012-01-05 19:31:40
我很抱歉没有使用评论部分,也许它应该在那里(没有足够的代表)。我只是想把我的2c研究投入到:
settings.
一切似乎都如期而至。
https://stackoverflow.com/questions/7827632
复制相似问题