is there a correct or accepted way to deal with assert failure in debug / release mode?
For instance:
let’s say I have a function that returns a std::vector
I expect the length of the returned vector to be equal to another object’s, and I do this:
std::vector<int> v = get_stuff();
ASSERT(v.size() == this->size() )
a = v[this->size() - 1 ];
now this code will work fine if no assertion is triggered, and in debug this could crash but at least I would have the assertion failure warning me that something is wrong.
The problem would be in release mode, where there would be a silent crash.
Does this mean that I have to also check for this error in the release code and then handle it? it’s possible but then I see no point in adding an assertion failure over it, since it is handled
An
asserthas a different meaning than you appear to think. It’s not a substitute for an exception, which I think is how you think of it. It’s there so that you can spot trouble early on in debug. If you get the assert in debug, you fix it. Then you test. And then test again. And make sure the condition would hold in release. If it does… well, you’ve got a bug.If you expect it to happen, yes. Check the condition, throw an exception, and handle it delicately. Send an error report. Write to a log file. Update the software.