2

On Visual Studio if I create a new Windows Form and put the following into the Form_Load event:

throw new Exception();

Nothing happens. I can put a breakpoint here and see that this line of code is reached, but no exception seems to be thrown. If I put the same statement into the click event of a button and the click the button, the program crashes as I would expect it to.

What is going on here?

Thanks

ispiro
  • 26,556
  • 38
  • 136
  • 291
JMK
  • 27,273
  • 52
  • 163
  • 280
  • 1
    Duplicate of [this](http://stackoverflow.com/questions/1583351/silent-failures-in-c-seemingly-unhandled-exceptions-that-does-not-crash-the-pr): looks like its a known bug. Also note that it will only occur in debug mode. – SimpleVar Jul 22 '12 at 12:38
  • @YoryeNathan So it is! Thanks for the link! – JMK Jul 22 '12 at 12:39
  • There is also a workaround found [here](https://connect.microsoft.com/VisualStudio/feedback/details/357311/silent-exceptions-on-x64-development-machines): Put a call to System.Diagnostics.Debugger.Break() in the beginning of OnLoad then run the application without debugging (Ctrl+F5). This causes NativeWindow.windowProc to == NativeWindow.Callback (instead of NativeWindow.DebuggableCallback) during the window's handle creation, so exceptions raised during OnLoad will be displayed. Once Break() is called, Visual Studio can attach to the process and you can debug as normal. – SimpleVar Jul 22 '12 at 12:42
  • 1
    possible duplicate of [VS2010 does not show unhandled exception message in a WinForms Application on a 64-bit version of Windows](http://stackoverflow.com/questions/4933958/vs2010-does-not-show-unhandled-exception-message-in-a-winforms-application-on-a) – Hans Passant Jul 22 '12 at 12:54
  • @HansPassant I am indeed asking the same question, your answer on that question made a lot of sense, thanks! – JMK Jul 22 '12 at 14:08

1 Answers1

1

If you run it outside the debugger it is thrown. You need to configure the debugger to break on the un-handled exception.

Debug->Exceptions...
Common Language Runtime Exceptions
   System
      System.Exception    Click the "Thrown" checkbox.
Steve Wellens
  • 20,506
  • 2
  • 28
  • 69
  • OK, but what is the difference between `Thrown` and `User-unhandled`? Will doing this have any knock on effects? – JMK Jul 22 '12 at 12:52
  • Thrown means break immediately when the exception is thrown. User-unhandled means break only if your code doesn't handle the exception. It's in the on-line help. – Steve Wellens Jul 22 '12 at 13:21