19

When running my application in VS2013 I get the exceptions:

A first chance exception of type 'System.InvalidOperationException' occurred in mscorlib.dll

and

A first chance exception of type 'System.InvalidOperationException' occurred in mscorlib.dll

This exceptions are caught anywhere since my application does not crash, everything works "normal". But I would really like to find the code section causing this exceptions to solve this.

What can I do to find out which part of my code throws this exception or where it happens. Is there a way to show/visualize this in VS? Debugger options?

ck84vi
  • 1,556
  • 7
  • 27
  • 49
  • 6
    This post will answer your query,http://stackoverflow.com/questions/4393092/c-sharp-a-first-chance-exception-of-type-system-invalidoperationexception – Ashish Sapkale Dec 09 '15 at 06:48
  • You can get the debugger to stop by ticking the Thrown checkbox. Actually seeing the code that threw the exception, that is much less likely to happen since you don't have the source code for mscorlib.dll. You'll have to reverse-engineer it from the content of the Call Stack debugger window and the Reference Source web site. – Hans Passant Dec 09 '15 at 07:59

1 Answers1

39

What can I do to find out which part of my code throws this exception or where it happens. Is there a way to show/visualize this in VS? Debugger options?

In Visual Studio, choose Debug then Exceptions.

In the Exceptions window, tick Thrown on the Common Language Runtime Exceptions row.

enter image description here

Click OK.

Now whenever an exception is thrown, the debugger will break on the offending line. You can also expand the selection to only include the exceptions of interest.

Visual Studio 2017

You can find this under Debug.Windows.Exception Settings

enter image description here

enter image description here

  • 13
    This window has been moved in VS2015. It's located at Debug -> Windows -> Exception Settings. – Daryl May 12 '16 at 12:33
  • 2
    Thanks @Daryl. Another fine example of _who moved my cheese?!_. ;) –  May 13 '16 at 00:21
  • I also didn't realize until today that there is a difference between these check boxes being "filled in" vs "checked" when they have sub-items. I guess filled in means that you can check the sub-items. Checked means all sub-items are also checked. That explains why VS wasn't breaking on any exceptions for me. – drewmerk Jul 26 '18 at 22:19
  • 1
    @drewmerk yes that’s right. I think I first saw the concept with installers and choosing which components to install. –  Jul 27 '18 at 00:06
  • 1
    If only every SO comment/solution was as detailed and helpful as this one. Kudos and thank you, @mickyd. – Chase Apr 29 '21 at 11:15
  • 1
    @Chase Thanks very much! My pleasure :) –  Apr 29 '21 at 12:39