From time to time (unfortunately way too often) I have to fix code such as this:
// C++ code
bool anyOldFunction(Param p) {
try {
...
if(some_condition_here) {
handleErrorX();
return false;
} else if(other_condition) {
return false; // (*stackexchange question comment*) Apparently no additional error action taken here
}
...
return true;
} catch(...) {
// (*old comment*) Catch all possible error (FroobleFub and BrabbleBub can happen)
return false;
}
}
So, the original implementer actually had some inkling what exceptions could be thrown (although the list may not be exhaustive) but had no idea how to do some decent handling and just discarded any info he might have gotten out of the exception objects, leaving the calling code / the calling context non-the-wiser.
Is there a name for this anti-pattern? (If anti-pattern it is indeed.)
true
on success andfalse
on failure, the caller will get exactly what it is expecting. Altering the code to start throwing exceptions at callers that were written to assume there won't be any could cause a working system to break, and you'll have to kick the can up the road and swallow the exceptions elsewhere.