3

I have a strange problem with Asserts being ignored. You can reproduce it with this minimal example. I am wondering why this problem occurs and how to face it:

public class TestAssert
{
    public string EmptyString
    {
        get
        {
            System.Diagnostics.Debug.Assert(false);
            return string.Empty;
        }
    }
    Dictionary<string, object> dict = new Dictionary<string, object>();

    public void ShowAssertIgnored()
    {
        var foo = dict[EmptyString];
    }
}    

You can see that the Debug.Assert(false) is ignored even though the property is evaluated. Just call

var test = new TestAssert();
test.ShowAssertIgnored();

and you should see what I mean (also shown on the image).Debug.Assert is not evaluated but the property is

The code is compiled and run in Debug (other asserts work fine!), 32 bit, x86 + AnyCPU, VS2012 professional, .Net Framework 4

Edit: The project is a console application and I was running it for several times in a row. Having a breakpoint before System.Diagnostics.Debug.Assert(false); most often a messagebox appears. But not always: When I just retry the same situation several times I sometimes see the result in the console.

To say it again: I can reproduce non-deterministic behaviour in the VS2012 debugger!

8
  • Does step-by-step debugging pass through the Debug.Assert line? Have you tried putting a breakpoint there? Commented Nov 26, 2015 at 13:12
  • Try to recreate the issue in a new project. I just tried with a simple console app and it hit the assert. Therefore, there's nothing specifically wrong with your code as presented in the question. There must be some other issue in your environment/vs setup Commented Nov 26, 2015 at 13:18
  • What's the project type of the calling code? What's the project type of the code that this is in? console app / winforms / class? Where are the 'other asserts' that work defined? What if you add one in ShowAssertIgnored()? Commented Nov 26, 2015 at 13:23
  • did a coworked, as a joke, add #undef DEBUG in your file ? Commented Nov 26, 2015 at 13:26
  • There is no #undef DEBUG and other Asserts work. But as you can see int the edit I have different results for different runs. Commented Nov 26, 2015 at 14:24

3 Answers 3

8

If you read the docs, you'll see that Debug.Assert isn't designed to throw.

Typically, the Assert(Boolean) method is used to identify logic errors during program development. Assert evaluates the condition. If the result is false, it sends a failure message to the Listeners collection. You can customize this behavior by adding a TraceListener to, or removing one from, the Listeners collection.

When the application runs in user interface mode, it displays a message box that shows the call stack with file and line numbers. The message box contains three buttons: Abort, Retry, and Ignore. Clicking the Abort button terminates the application. Clicking Retry sends you to the code in the debugger if your application is running in a debugger, or offers to open a debugger if it is not. Clicking Ignore continues with the next instruction in the code.

If running in an UI environment, it may trigger throw-like behaviour that is defined in the listeners that the UI environment/test-runner sets up.

So, in short, Debug.Assert(false) won't halt your application but its listeners might.

Assuming that there is no UI here... If you want your code to fail, then you'll need to write your own TraceListener:

public class MyTraceListener : TraceListener
{
    public override void Write(string msg)
    {
        throw new Exception(msg);
    }
    public override void WriteLine(string msg)
    {
        throw new Exception(msg);
    }
}

and add it to the listeners collection:

Debug.Listeners.Add(new MyTraceListener());
Sign up to request clarification or add additional context in comments.

2 Comments

I am running a Program with UI and expect the messagebox to appear. However, NOT ALWAYS this messagebox appears. Sometimes it just prints to the console. However, I am running the same settings without any change.
Why do you assume that throw behavior is desiderd? The issue is that Debug.Assert is ignored, not that it does not throw exceptions. In fact, this is a recurrent bug in Visual Studio (2010 for me): after some undefined time (or after an undefined event occurs), Debug.Assert stop working. Sometimes a VS restart is required to get them working as expected.
1

Have you checked your project settings to define the DEBUG constant? The Debug.Assert() methods have the [ConditionalAttribute("DEBUG")] attribute, which means, they will by ignored during compilation, if the "DEBUG" constant is not defined.

4 Comments

As stated by freedomn-m the code is compiled and run as Debug
the build configuration named "Debug" does not necessarily define the DEBUG constant!
At some point the problem disappeared. However, I am not aware of the change I did. Maybe this idea is correct and I didn't have the DEBUG constant defined. Thank you for the input.
Just hit this with Visual Studio 2019 (16.11.7) with some .NET 5.0 C# projects which have had "Define DEBUG constant" checked for years in their project properties without <DefineConstants>DEBUG</DefineConstants> in their .csproj files. Something caused the setting become unchecked and manually rechecking it inserted <DefineConstants> in the projects. Since the VS installation and solution nugets were stable I've no plausible explanation for why this might occur.
0

That's an expected behaviour, Debug.Assert() works in debug build only:

By default, the Debug.Assert method works only in debug builds. Use the Trace.Assert method if you want to do assertions in release builds. For more information, see Assertions in Managed Code.

https://msdn.microsoft.com/en-us/library/kssw4w7z(v=vs.110).aspx

use Trace.Assert() if you want it working in release as well.

2 Comments

"The code is compiled and run in Debug" quote from OP
As stated by freedomn-m the code is compiled and run as Debug

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.