Catch block not catching exception

2019-01-18 00:30发布

问题:

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)

回答1:

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.



回答2:

I have the same problem. What I eventually did was to catch all exceptions. In C#:

Application.ThreadException += new ThreadExceptionEventHandler(MyHandler);

And then show the form.

I'd love to hear if anyone has a better solution.



回答3:

I apologize for the C# (I don't know the Vb syntax)

are you doing something like this:

  ChildForm child = new ChildForm();
  try {
      child.Show();
  }
  catch(Exception ex)
  {.....}

If so, I believe the Load event would happen on the New, not the Show(); (Show would fire Activate)



回答4:

The new window has its own thread, which is doing its own loading. To verify this, you can try putting in a Thread.Sleep for a few seconds into Form2_Load before the exception. Your main thread window should continue execution before you hit the exception.