Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Debug.Assert() only triggers when stepping over it

I'm using Visual Studio 2010 to write a .NET assertion:

Debug.Assert(false, "Testing Debug.Assert");

When I put a breakpoint on this line of code, wait until the breakpoint hits, and then let the program continue to run, everything works fine: an "Assertion Failed" dialog pops up. However, when I remove the breakpoint and rerun the application, the Debug.Assert() statement is simply ignored.

Does anyone have an idea what might cause this very strange behavior?

like image 412
Dimitri C. Avatar asked Oct 21 '11 09:10

Dimitri C.


2 Answers

A colleague of mine immediately knew what the problem was. I had to enable the "Enable Just My Code" flag at "Visual Studio 2010 / Menu / Options / Debugging / General". Apparently this is a bug in Visual Studio 2010".

like image 181
Dimitri C. Avatar answered Oct 13 '22 10:10

Dimitri C.


I was experiencing the same problem. Using Trace.Assert works as expected.


Well, Trace.Assert started exhibiting the same behavior on the next debugging session.


Ah, found the problem.

  • With 'Enable Just My Code" checked.

    • If 'Thrown' is checked for 'Common Language Runtime Exceptions' in the 'Exceptions' dialog (Ctrl+Alt+E), the Assert will be thrown.

    • If unchecked, the Assert will be thrown.

  • With 'Enable Just My Code" unchecked.

    • If 'Thrown' checked, the Assert will not be thrown.

    • If 'Thrown' unchecked, the Assert will be thrown.

Therefore, the Assert will not be thrown when 'Enable Just My Code' is unchecked and 'Thrown' is checked for 'Common Language Runtime Exceptions'.

like image 41
AMissico Avatar answered Oct 13 '22 12:10

AMissico