views:

233

answers:

3

Hi, I really want to be able to go: (in C++)

assert( num > 0, "The number must be greater than zero!");

In C# XNA, they have a method that does exactly this:

Debug.Assert( num > 0, "The number must be greater than zero!");

Is there some way to do this so that the runtime gives me a meaning full error not just "an assertion failed" error?

+4  A: 

Use assert( num > 0 && "The number must be greater than zero!" );

Mykola Golubyev
+5  A: 

The runtime usually displays the expression that failed, so you can do something like this:

assert(num > 0 && "The number must be greater than zero!");

Which will not affect the logic, but the assertion failure message will contain a descriptive string.

In addition, this works without needing the STL or Boost.

Ferruccio
+7  A: 

You can use this in most any assertion:

assert(("The number must be greater than zero!",  num > 0));
Eclipse
+1 - I like that better than my solution.
Ferruccio
LOL so simple.Thank you! Accepted answer :D
Brock Woolf