0

This doesn't really concern any code specifically but rather the development process. Every now and then, I bork up and something called from MyBase.Load throws an exception.

Unlike exceptions triggered by program flow that starts somewhere else.. like a button for example, those exceptions don't stop the program and don't start the debugger in the appropriate place.

This is why I usually develop .net applications with the .Load code residing in a button instead, until its ready and I can finally move it into where it really should be.

For obvious reasons.. this is annoying. Is there any way to get around this? Some checkbox to tick.. or maybe use a different event to detect program start?

user81993
  • 6,167
  • 6
  • 32
  • 64

1 Answers1

0

Exceptions thrown inside Load event work differently. It has nothing to do with Visual Studio, it is by design of Microsoft Windows.

The cause: calling context of Load method handler is simply said "in different universe". Thrown exception goes up through different paths and does not reach the application in typical way. More information – recommended reading.

There is a way around this:

Never use a Load event

except of resizing controls what cannot be effectively done elsewhere.

Edither override the OnLoad() method or put most of your code into the constructor instead. And if some code needs the form to be completely initialized, handle it in Shown event.

Side note: I wonder why desingers of Visual Studio still offer OnLoad event handler if you double-click a form in designer. This leads the path of inexperienced developers into nasty errors (especially on exceptions) based on the above facts.

Community
  • 1
  • 1
miroxlav
  • 11,796
  • 5
  • 58
  • 99