Is there a way to break the debugger when assertion is false and running the application using Visual Studio debugger. Earlier when I was debugging Windows application I would get an exception and the debugger would break, but now on Smart Device an assertion failed window is showed with stack trace, but I would also like to see variable values etc.
5 Answers
Stupid me, the solution was simple. When the window pops out, press pause in debugger :)

- 2,274
- 5
- 26
- 34
-
3I wonder if there's any way to do this automatically, though. – Superbest Jan 30 '12 at 16:05
-
1@Superbest - There is. See my answer. – Darrel Hoffman Dec 26 '14 at 19:05
-
1When debugging a unit test using Visual Studio, Debug.Assert failure does not pop up a window, it's like the test framework has removed this feature. (I'm using VS 2013 Pro.) – yoyo Apr 14 '16 at 17:16
-
What if you don't have the debugger attached? – WeSam Abdallah Mar 06 '17 at 16:44
Not sure about VS 2008, but in at least 2010 and later, you can go to Debug/Exceptions (Ctrl-Alt-E). Click the "Add" button, choose "Common Language Runtime Exceptions", and type:
Microsoft.VisualStudio.TestTools.UnitTesting.AssertFailedException
and hit "OK". It will now appear on the list, make sure you check the checkbox in the "Thrown" column. You will now get a break on any assert failure.

- 1,977
- 19
- 44

- 4,436
- 6
- 29
- 41
-
Good tip, but what if I want to break on System.Diagnostics.Debug.Assert while debugging a unit test? – yoyo Apr 14 '16 at 00:25
-
Found a solution - use [TestInitialize] to add a custom System.Diagnostics.TraceListener that throws a UnitTesting.AssertFailedException in the event of a Debug.Assert failure. – yoyo Apr 14 '16 at 17:35
-
@yoyo: _"what if I want to break on System.Diagnostics.Debug.Assert while debugging a unit test"_ -- calling that method with a value of `false` for the `condition` parameter will, according to the documentation, display a dialog providing three options: "Abort", "Retry", and "Ignore". Choosing "Retry" will break in the debugger (similar to calling `Debugger.Break()`). There's no need to involve exception handling at all; the `Assert()` method is completely unrelated to exception handling and breaking in the debugger for `Assert()` involves a completely different mechanism. – Peter Duniho Jul 07 '20 at 23:05
In addition to Vinay's solution, you can start the debugger for a specific process by calling
In your case you could do it every time the listener receives a failure message.

- 37,131
- 7
- 73
- 89
It seems that you can attach the Debugger when assertion fails to see other details - see this article: http://blogs.msdn.com/b/davidklinems/archive/2005/08/29/457847.aspx. Its quite dated but perhaps still applicable.

- 47,395
- 5
- 59
- 72
For a Native Unit Test project (C++), we can follow a method similar to the one in @Darrel Hoffman's answer.
Go to Debug->Windows->Exception Settings
.
Add a new exception under Win32 Exceptions
.
For error code enter 0xE3530001
, and give it some description.
Now, from test explorer, select
Debug
, instead of Run
.
Alternatively, it will work even if you select <All Win32 Exceptions not in this list>
.
Visual Studio will now break whenever a Native Unit Test assertion (Assert::IsTrue, Assert::IsFalse, etc.) fails.
Note that by default breaking on 0xc0000420 Assertion failed
exception is enabled in Visual Studio exception settings, however it doesn't cause the debugger to break when our unit test assertion fails, hence the steps above are necessary.
PS: @Darrel Hoffman' answer is sufficient for C# unit tests, but for native C++ a few extra steps are required, hence my answer may help those like me who stumble upon this for native use case.
References:

- 3,352
- 39
- 62