Block Exception Lock

I have a child form that throws 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 the message and closes the child form. Everything works as expected when debugging in visual studio 2008 (.net 3.5 sp1). However, when I run it outside of the visual studio, the Catch block seems to be skipped and an unhandled exception is thrown. Any idea why this is?

Thanks.

Code example:

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)
+5
4

Form.Load , Windows Forms. , , Windows WM_SHOWWINDOW. , . Application.ThreadEvent. .

, , Load Click handler. , , . - Initialize(). Load . Initialize() Show(), .

+11

. , , - . #:

Application.ThreadException += new ThreadExceptionEventHandler(MyHandler);

.

, - .

+1

# ( Vb)

- :

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

If so, I believe that the Load event will occur in New, and not in Show (); (Show will light Activate)

0
source

The new window has its own stream, which performs its own download. To verify this, you can try to put Thread.Sleepin Form2_Load for a few seconds before the exception. The main thread window must continue before you hit the exception.

0
source

All Articles