【发布时间】:2010-09-28 05:14:38
【问题描述】:
我有一个在 Load 事件处理程序中抛出 ApplicationException 的子表单(故意用于测试目的)。父窗体将 ChildForm.Show() 方法包装在 Try...Catch ex As Exception 块中。 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: 在 WindowsApplication1.Form2.Form2_Load 测试 Form_Load(Object sender, EventArgs e) 在 UnhandledExceptionTest2\WindowsApplication1\Form2.vb System.Windows.Forms.Form.OnLoad(EventArgs e) System.Windows.Forms.Form.OnCreateControl() System.Windows.Forms.Control.CreateControl(布尔 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 参数)【问题讨论】:
-
是的——描述的很模糊......
标签: .net vb.net visual-studio visual-studio-2008 .net-3.5