4

I just came across odd behavior with exception handling in .Net. (I'm using C# in MS Visual Studio 2008, but one question I saw here seemed to imply that what I see is true throughout the .Net world.) I am writing a plain WinForm application. I am intentionally causing an unhandled exception to be thrown inside a form_load event handler, outside of any try block. I get no notification. If an unhandled exception occurs in a normal method, a message pops up telling me that the exception happened, and giving me some information about the problem. But in the handler, the code just quietly exits the function without letting anybody know that it happened. If I add a try/catch block, the exception is caught as expected.

Is it true that this behavior happens in all event handlers? And is this expected behavior? And if so, is it because there is too much danger of bad things happening if an event handler unexpectedly stops?

psubsee2003
  • 8,563
  • 8
  • 61
  • 79
ROBERT RICHARDSON
  • 2,077
  • 4
  • 24
  • 57
  • Have you possibly created an UnhandledThreadExceptionEventHandler? – Pieter Geerkens Mar 07 '13 at 21:19
  • From what I read, it's seen to be the same problem as this [question]http://stackoverflow.com/questions/10647551/exceptions-not-being-raised-in-c-just-kicks-out-of-the-routine-is-this-a-bug/10652522#10652522 – Stephan Mar 07 '13 at 21:26
  • Thank you for your reply. The question you referenced appears to talk about behavior when debugging inside Visual Studio. I observed this behavior both inside and outside VS. I will admit, though, that both times was a debug build. I did not try my test in a release build. – ROBERT RICHARDSON Mar 08 '13 at 15:29

1 Answers1

1

Whether inside or outside VS, this behavior occurs when there is a debugger attached to the process. However, being a debug version makes no difference. If running outside VS without a debugger attached, the unhandled exception will fire up. You can check

Why the form load can't catch exception? , and VS2010 does not show unhandled exception message in a WinForms Application on a 64-bit version of Windows , for possible solutions.

EDIT: This behavior is only specific to the form_load event handler, as far as I know.

Community
  • 1
  • 1
ThunderGr
  • 2,297
  • 29
  • 20