Catch块没有捕获异常 [英] Catch block not catching exception
问题描述
我有一个抛出ApplicationException的在Load事件处理程序(有意用于测试目的)子窗体。父形式包装在一个try ... catch EX作为异常块ChildForm.Show()方法。 catch块只显示一条消息,并关闭子窗体。所有工作按预期在Visual Studio 2008(.NET 3.5 SP1)调试。然而,当我运行它的Visual Studio之外,catch块似乎变得遗漏,并发生未处理的异常。任何想法,这是为什么?
感谢你。
例如code:
公共类Form1中
私人小组的button1_Click(BYVAL发件人为System.Object的,BYVALË作为System.EventArgs)把手Button1.Click
昏暗的F2作为窗体2
F2 =新窗体2
尝试
的MessageBox.show(打开表2)
f2.ShowDialog()
抓住EX为例外
f2.Close()
的MessageBox.show(表2关闭。)
结束尝试
结束小组
末级
公共类窗体2
私人小组Form2_Load(BYVAL发件人为System.Object的,BYVALË作为System.EventArgs)把手MyBase.Load
抛出新ApplicationException的(测试的Form_Load)
结束小组
公共子新()
'该调用是Windows窗体设计器。
的InitializeComponent()
在InitializeComponent()调用后'添加任何初始化。
结束小组
末级
堆栈跟踪:
System.ApplicationException: 测试的Form_Load在WindowsApplication1.Form2.Form2_Load(对象发件人,EventArgs的) 在UnhandledExceptionTest2 \的WindowsApplication1 \ Form2.vb System.Windows.Forms.Form.OnLoad(EventArgs的五) System.Windows.Forms.Form.OnCreateControl() System.Windows.Forms.Control.CreateControl(布尔fIgnoreVisible) System.Windows.Forms.Control.CreateControl() System.Windows.Forms.Control.WmShowWindow(消息&M)在 System.Windows.Forms.Control.WndProc(消息&> m)上 System.Windows.Forms.ScrollableControl.WndProc(消息&> m)上 System.Windows.Forms.ContainerControl.WndProc(消息&> m)上 System.Windows.Forms.Form.WmShowWindow(消息&> m)上 System.Windows.Forms.Form.WndProc(消息&> m)上 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(消息&> m)上 System.Windows.Forms.Control.ControlNativeWindow.WndProc(消息&> m)上 System.Windows.Forms.NativeWindow.Callback(IntPtr的的HWND,味精的Int32,IntPtr的WPARAM,IntPtr的 LPARAM)
在Form.Load事件的行为同样的方式在Windows窗体大多数其他事件。它是由消息循环调度中,当Windows发送WM_SHOWWINDOW消息,这种情况下。有在prevents未捕获的异常终止,从消息循环消息循环异常处理程序。这异常处理程序引发Application.ThreadEvent事件。默认的事件处理程序显示未处理的异常对话。
长话短说,你不能赶在你的按钮单击处理程序提出的Load事件异常。除了捕获和处理异常的Load事件处理程序本身,很难做到正确的,我建议你添加一个公共方法的形式。像初始化()。从Load事件到该方法将code。调用Show()方法后调用初始化(),例外的是现在的你赶上。
I have a child form that is throwing an ApplicationException in the Load event handler (intentionally for testing purposes). The parent form wraps the ChildForm.Show() method in a Try...Catch ex As Exception block. The catch block simply displays a message and closes the child form. All works as expected when debugged in visual studio 2008 (.net 3.5 sp1). However, when I run it outside of visual studio, the Catch block seems to get missed and an unhandled exception occurs. Any idea why that is?
Thank you.
Example Code:
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
Stack Trace:
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)
The Form.Load event behaves the same way as most other events in Windows Forms. It is dispatched by the message loop, in this case when Windows sends the WM_SHOWWINDOW message. There is an exception handler in the message loop that prevents an uncaught exception from terminating the message loop. That exception handler raises the Application.ThreadEvent event. The default event handler displays the unhandled exception dialog.
Long story short, you cannot catch an exception raised in the Load event in your button Click handler. Other than catching and handling exceptions in the Load event handler itself, very hard to do right, I'd recommend you add a public method to the form. Something like Initialize(). Move the code from your Load event into that method. Call Initialize() after calling the Show() method, exceptions are now yours to catch.
这篇关于Catch块没有捕获异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!