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() meth
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.
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.
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)
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.