我有一个在Load事件处理程序中抛出ApplicationException的子表单(故意用于测试目的)。父窗体在Try ... Catch ex As Exception块中包装ChildForm.Show()方法。 catch块只是显示一条消息并关闭子窗体。所有在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: Test Form_Load at WindowsApplication1.Form2.Form2_Load(Object sender, EventArgs e) in UnhandledExceptionTest2\WindowsApplication1\Form2.vb 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) at System.Windows.Forms.Control.WndProc(Message&> m) at System.Windows.Forms.ScrollableControl.WndProc(Message&> m) at System.Windows.Forms.ContainerControl.WndProc(Message&> m) at System.Windows.Forms.Form.WmShowWindow(Message&> m) at System.Windows.Forms.Form.WndProc(Message&> m) at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message&> m) at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&> m) at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
你能发表一些代码吗? – 2008-12-23 17:24:48
是的 - 它描述的含义很模糊...... – 2008-12-23 17:27:59