Search code examples
c#exceptionfirst-chance-exception

Skip first chance exceptions


I've inherited a project that, when started in debug, throws somewhere in the avenue of 8-10k NotImplementedExceptions. Because of this, the application takes over a minute to start when debugging... Every. Single. Time.

I talked to the original devs of the project and their solution to the issue is "Just press Ctrl+F5 to start without attaching a debugger". Apart from that being one of the worst solutions to a problem in the history of development, it does work and the application starts immediately. I'm trying to learn this new code-base so just skipping the debugger isn't an option for me.

Obviously I'm aware that no application should start with 10 thousand exceptions, but before I even get to fixing that I have to be able to debug the program.

I would like to suppress or skip first chance exceptions in the same way that starting without the debugger attached does. I've looked at this thread and applied the [DebuggerNonUserCode] attribute to the related methods but it only prevented the exceptions from being written to the output window. The program still takes over a minute to start up. Is this possible?

Edit:

I forgot to mention, I don't have anything checked in the Debug->Exceptions window. Also, all of the exceptions are wrapped in Try.. Catch statements


Solution

    1. Check your Visual Studio settings under Debug -> Exceptions -> Common Language Runtime Exceptions -> System -> System.NotImplementedException and make sure that Thrown is NOT checked. (it should be not checked by default, but this would cause it to stop even if exception were to be handled).
    2. Crappy workaround: In the part of the code which is past all exceptions, where you want to start debugging, put the following lines

      System.Diagnostics.Debugger.Launch();
      System.Diagnostics.Debugger.Break();
      

    You can then start Ctrl+F5, and then be prompted to attach debugger when it hits that point in the code.