.net - Catch block not catching exception -
i have child form throwing applicationexception in load event handler (intentionally testing purposes). parent form wraps childform.show() method in try...catch ex exception block. catch block displays message , closes child form. works expected when debugged in visual studio 2008 (.net 3.5 sp1). however, when run outside of visual studio, catch block seems missed , unhandled exception occurs. idea why is?
thank you.
example code:
public class form1 private sub button1_click(byval sender system.object, byval e system.eventargs) handles button1.click dim f2 form2 f2 = new form2 try messagebox.show("opening form 2") f2.showdialog() catch ex exception f2.close() messagebox.show("form 2 closed.") end try end sub end class public class form2 private sub form2_load(byval sender system.object, byval e system.eventargs) handles mybase.load throw new applicationexception("test form_load") end sub public sub new() ' call required windows form designer. initializecomponent() ' add initialization after initializecomponent() call. end sub end class
stack trace:
system.applicationexception: test form_load @ 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) @ 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 lparam)
the form.load event behaves same way other events in windows forms. dispatched message loop, in case when windows sends wm_showwindow message. there exception handler in message loop prevents uncaught exception terminating message loop. exception handler raises application.threadevent event. default event handler displays unhandled exception dialog.
long story short, cannot catch exception raised in load event in button click handler. other catching , handling exceptions in load event handler itself, hard right, i'd recommend add public method form. initialize(). move code load event method. call initialize() after calling show() method, exceptions yours catch.
Comments
Post a Comment