Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Assert Macro that report failure value

Tags:

c++

assert

I have tons of run time assertion failure in my application and I need to sit with each one to find out what is the run time value of the assert conditions that results such a failure. For example:

assert ( a == b ) ; 

in line number 100 failed. in run time, I can only see that some thing happen in line number 100 then, I need to set a break point there to find out actual value of a and b.

My questions is that is there any way to get more intelligent failure report more than line numbers? I would like to see value of variables that are mismatch.

like image 494
ARH Avatar asked Sep 07 '26 10:09

ARH


1 Answers

You can utilize the language to define your own assert macros of course:

#define ASSERT_EQUAL(a,b) if ((a) != (b)) std::cout << "Assertion failed:" << (a) << "!=" << (b) << " at:" << __LINE__

However, I would argue that if you're relying excesively on assertions, you might want to express some of thes "exceptional" errors as exceptions. A good debugger will catch these and describe the exception by name. You may have something more meaningful to say rather than a != b, for example:

if (a != b) {
    throw InvalidArgumentsException(a, b);
}

While this is useful, its important to realize than exceptions get thrown in both debug and release builds while assertions typically only get run in debug builds.

like image 129
Doug T. Avatar answered Sep 10 '26 01:09

Doug T.