c++ 中的自定义(非例外)错误处理策略
出于 X 或 Y 原因,人们在必要时在 C++ 中使用哪些错误处理方案来避免异常?我已经实施了自己的策略,但我想知道其他人提出了什么,并就每种方法的优点和缺点的主题进行讨论
现在,为了解释我在特定项目上使用的方案,它可以这样概括。通常需要抛出的方法,实现一个接口,例如:
bool methodName( ...parameters.... , ErrorStack& errStack)
{
if (someError) { errStack.frames.push_back( ErrorFrame( ErrorType , ErrorSource ) );
return false;
}
... normal processing ...
return true;
}
简而言之,返回参数表示处理是否正常或发生错误。错误堆栈基本上是错误帧的 std::vector,其中包含有关错误的详细信息:
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();
}
};
这种方法的优点是类似于 java 异常所提供的详细错误堆栈,但没有异常的运行时开销。主要缺点是(除了非标准性并且我仍然必须以某种方式处理来自第三方代码的异常并转换为 ErrorCondition ),这是很难维护 ErrorCondition 枚举,因为源库的多个组件需要不同的错误,因此该策略的第二个版本可以使用某种继承层次结构来处理错误条件,但我仍然对实现它的最佳方法没有信心
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
查看 http://www.open-std.org/jtc1 /sc22/wg21/docs/TR18015.pdf 第 32 页起
check out http://www.open-std.org/jtc1/sc22/wg21/docs/TR18015.pdf Page 32 onwards