C++如何处理对象组合中出现的错误
C++ how to deal with error appeared deep in objects composition?
这里每个新方法调用都必须检查错误,这真的。。。至少要慢一点。我在考虑例外情况,但我读到它们不适合处理错误。例如,如果我要抛出异常,我必须使用堆栈上的上一个方法来处理它,因为否则可能会有局部变量指向该方法中分配的空间,所以我不能跳过它。那么,有什么好的方法吗?
class A {
int method() {
...;
return 1;
}
};
class B {
A a;
int method() {
int err = a.method();
if (err == 1) {
...;
return ret;
}
}
};
class C {
B b;
int method() {
int err = b.method();
if (err == 1) {
...;
return ret;
}
}
};
int main() {
C c;
int err = c.method();
if (err == 1) {}
return 0;
}
我可以想出以下方法来处理函数中深层的错误——无论它们是由对象组合还是函数层引起的,都是不相关的。
方法1:在低级别函数的深处抛出异常
struct my_error_A {};
class A {
void method() {
if ( ... )
{
throw my_error_A();
}
...;
}
};
struct my_error_B {};
class B {
A a;
void method() {
a.method();
...;
if ( ... )
{
throw my_error_B();
}
}
};
struct my_error_C {};
class C {
B b;
void method() {
b.method();
...;
if ( ... )
{
throw my_error_C();
}
}
};
int main() {
C c;
try
{
c.method();
}
catch (my_error_A err)
{
// Deal with errors from A.
}
catch (my_error_B err)
{
// Deal with errors from B.
}
catch (my_error_C err)
{
// Deal with errors from C.
}
catch (...)
{
// Deal with all othe exceptions.
}
return 0;
}
方法2:使用全局错误代码保持器来跟踪错误
int globalErrorCode = 0;
class A {
void method() {
a.method();
...;
if ( ... )
{
globalErrorCode = A::errorCode;
}
}
static int errorCode;
};
class B {
A a;
void method() {
a.method();
if ( globalErrorCode != 0 )
{
return;
}
...;
if ( ... )
{
globalErrorCode = B::error_code;
return;
}
...;
}
static int errorCode;
};
class C {
B b;
void method() {
b.method();
if ( globalErrorCode != 0 )
{
return;
}
...;
if ( ... )
{
globalErrorCode = C::error_code;
return;
}
...;
}
static int errorCode;
};
int main() {
C c;
c.method();
if ( globalErrorCode != 0 )
{
// Deal with error code.
}
return 0;
}
方法3:在每个级别返回错误代码,并在每个级别进行处理
class A {
int method() {
if ( ... )
{
return A::errorCode;
}
...;
}
static int errorCode;
};
class B {
A a;
int method() {
int err = a.method();
if ( err != 0 )
{
return err;
}
...;
if ( ... )
{
return B::errorCode;
}
}
static int errorCode;
};
class C {
B b;
int method() {
int err = b.method();
if ( err != 0 )
{
return err;
}
...;
if ( ... )
{
return C::errorCode;
}
}
static int errorCode;
};
int main() {
C c;
int err = c.method();
if ( err != 0 )
{
// Deal with errors
}
return 0;
}
class A
{ void method()
{
}
};
class B
{ A a;
void method()
{ a.method();
}
};
class C
{ B b;
void method()
{ b.method();
}
};
int main()
{ try
{ C c;
c.method();
return 0;
} catch (const std::exception &e)
{ std::cerr << "Failure: " << e.what() << std::endl;
return 1;
}
}
如果你有指向本地对象的指针,而指针的寿命可能比指向的对象长,那么你做的事情就大错特错了。这样的设计会导致崩溃。
相关文章:
- 警告处理为错误这里有什么问题
- 错误:(-210:不支持的格式或格式组合)功能'create'中的硬件视频解码器不支持视频源
- Freeopcua C++ 客户端和 Python Opcua 的组合会在 getChild() 上抛出错误
- 使用to_string、reverse、stoi组合的C++反转编号给出运行时错误实例超出范围
- 组合 lambda 捕获时出现编译错误
- OpenCV 错误的解决方案:不支持的格式或格式组合 - 当将 ORB 功能与 FlannBasedMatcher 匹配
- 类组合中的构造函数错误
- 将PCH,PDB和Zi组合在一起会导致VS2017的C2859编译错误令人费解
- C Fstream字符串组合错误
- 覆盖组合对象的<<运算符会给出错误 C++
- OpenCV错误:不支持的格式或格式组合(Point3f类型的点的矢量的矢量)
- 组合框初始化错误:无法读取未定义的属性'constructor'
- 将glUniformMatrix与数组组合使用时出现分割错误
- 试图将字符串的两个元素组合在一起会得到错误的结果(c++)
- 使用C++进行 OpenCv 相机校准(不支持的格式或格式组合错误)
- 寻找c++快速排序/插入排序组合中的错误
- 错误:类计算机组合已在 computer.obj 中定义
- QString和stdstring组合在std::编译错误中不起作用
- C++如何处理对象组合中出现的错误
- 怎么了?-组合编译错误(与家庭+人类的例子)