If I use assert() and the assertion fails then assert() will call abort(), ending the running program abruptly. I can’t afford that in my production code. Is there a way to assert in runtime yet be able to catch failed assertions so I have the chance to handle them gracefully?
If I use assert() and the assertion fails then assert() will call abort() ,
Share
Yes, as a matter of fact there is. You will need to write a custom assert function yourself, as C++’s
assert()is exactly C’sassert(), with theabort()‘feature’ bundled in. Fortunately, this is surprisingly straightforward.Assert.hh
The above function will throw an exception if a predicate doesn’t hold. You will then have the chance to catch the exception. If you don’t catch the exception,
terminate()will be called, which will end the program similarly toabort().You may wonder what about optimizing away the assertion when we’re building for production. In this case, you can define constants that will signify that you’re building for production and then refer to the constant when you
Assert().debug.hh
main.cc
If
CHECK_WRONGis a constant then the call toAssert()will be compiled away in production, even if the assertion is not a constant expression. There is a slight disadvantage in that by referring toCHECK_WRONGwe type a little more. But in exchange we gain an advantage in that we can classify various groups of assertions and enable and disable each of them as we see fit. So, for example we could define a group of assertions that we want enabled even in production code, and then define a group of assertions that we only want to see in development builds.The
Assert()function is equivalent to typingbut it clearly indicates the intent of the programmer: make an assertion. Assertions are also easier to grep for with this approach, just like plain
assert()s.For more details on this technique see Bjarne Stroustrup’s The C++ Programming Language 3e, section 24.3.7.2.