我有一个子表单,在Load事件处理程序中故意抛出ApplicationException(用于测试目的)。父表单将ChildForm.Show()方法包装在Try...Catch ex As Exception块中。当在Visual Studio 2008(.net 3.5 sp1)中进行调试时,所有工作都按预期进行。但是,当我在Visual Studio之外运行它时,似乎会跳过Catch块,并发生未处理的异常。您有任何想法吗?
谢谢。
示例代码:
谢谢。
示例代码:
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim f2 As Form2
f2 = New Form2
Try
MessageBox.Show("Opening form 2")
f2.ShowDialog()
Catch ex As Exception
f2.Close()
MessageBox.Show("Form 2 closed.")
End Try
End Sub
End Class
Public Class Form2
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Throw New ApplicationException("Test Form_Load")
End Sub
Public Sub New()
' This call is required by the Windows Form Designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
End Sub
End Class
堆栈跟踪:
System.ApplicationException: 在 UnhandledExceptionTest2\WindowsApplication1\Form2.vb 的 WindowsApplication1.Form2.Form2_Load(Object sender, EventArgs e) 处发生错误 System.Windows.Forms.Form.OnLoad(EventArgs e) System.Windows.Forms.Form.OnCreateControl() System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible) System.Windows.Forms.Control.CreateControl() System.Windows.Forms.Control.WmShowWindow(Message& m) 在 System.Windows.Forms.Control.WndProc(Message& m) 处 System.Windows.Forms.ScrollableControl.WndProc(Message& m) 在 System.Windows.Forms.ContainerControl.WndProc(Message& m) 在 System.Windows.Forms.Form.WmShowWindow(Message& m) 在 System.Windows.Forms.Form.WndProc(Message& m) 处 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) 在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) 处 System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)