Is it possible to write a message on assert error?
For example, using #include <assert.h>
, I can only write:
void foo(int a, int b) {
assert (a != b);
}
However, on an error I want to see the value of a
and b
. How can I do that?
Is it possible to write a message on assert error?
For example, using #include <assert.h>
, I can only write:
void foo(int a, int b) {
assert (a != b);
}
However, on an error I want to see the value of a
and b
. How can I do that?
I usually simply use assert(a != b && "This is my error message")
. This works because char*
can be converted to bool and that it never will be false (since the address is not 0).
assert
is just a preprocessor macro, that if the condition fails calls abort
. You can easily make your own with a simple if
statement, and if it fails print the values and call abort
just like assert
:
void foo(int a, int b)
{
if (a != b)
{
std::cerr << "My own assertion failed: a = " << a << ", b = " << b << '\n';
abort();
}
/* ... */
}
However, as you are using C++ you should probably thrown an exception instead, probably std::logic_error
:
void foo(int a, int b)
{
if (a != b)
{
std::istringstream is;
is << "Argument mismatch in `foo`: a = " << a << ", b = " << b;
throw std::logic_error(is.str());
}
/* ... */
}
It is possible to write your own assert macro which can break down an expression into subexpressions and print the values of each; but it requires a lot of code to support it, and the standard assert
macro doesn't do that on any implementation I know of.
For an example of how to implement such a thing, see the CATCH test framework, in particular the implementation of the CHECK
macro.
Alternatively, you could write macros or functions like ASSERT_EQUAL(a,b)
, giving each subexpression as a separate argument.
Or you might write your own error-handling code without assert
if (a == b) {
throw std::runtime_error("foo: a(" + std::to_string(a) + ") must not equal b");
}