What error handling schemes people use in c++ when its necessary, for X or Y reason, to avoid exceptions? I've implemented my own strategy, but i want to know what other people have come up with, and bring discussion on the topic about benefits and drawbacks of each approach
Now, to explain the scheme i'm using on a particular project, it can be summed up like this. Methods that would normally require to throw, implement an interface like:
bool methodName( ...parameters.... , ErrorStack& errStack)
{
if (someError) { errStack.frames.push_back( ErrorFrame( ErrorType , ErrorSource ) );
return false;
}
... normal processing ...
return true;
}
in short, the return parameter says if the processing was ok or an error occurred. the Error Stack is basically a std::vector of error frames that contain detailed information about the error:
enum ErrorCondition {
OK,
StackOverflowInminent,
IndexOutOfBounds,
OutOfMemory
};
struct ErrorFrame {
ErrorCondition condition;
std::string source;
ErrorFrame( ErrorCondition cnd , const char* src ) : condition(cnd) , source(src) {}
inline bool isOK() const {
return OK == condition;
}
};
struct ErrorStack {
std::vector< ErrorFrame > frames;
void clear() {
frames.clear();
}
};
The advantage of this approach is a detailed stack of errors similar to what java exceptions give, but without the runtime overhead of exceptions. The main drawback is that (besides the non-standardness and that i still have to handle exceptions from third-party code somehow and tranlate to an ErrorCondition), is that is hard to mantain the ErrorCondition enum, since multiple components of the source base require different errors, so a second version of this strategy could use a inheritance hierarchy of some sort for the errorConditions, but i'm still not confident about the best way to achieve it