【发布时间】:2016-10-07 15:52:12
【问题描述】:
我有一个类接口函数,它以特定顺序在类中实现其他函数:
class Child
{
public:
auto Interface()->bool
{
this->F1(); //I use this just for extra clarity (e.g. not calling global function)
this->F2();
return true;
}
auto F1()->void
{
//Do stuff...
}
auto F2()->void
{
//Do more stuff...
}
};
class Parent
{
public:
Child ChildObj;
auto CallUponChild()->void
{
bool success = ChildObj.Interface();
}
};
我想将 'Interface()' 实现包装在 try/catch 块中:
auto Interface()->bool
{
try{
this->F1();
this->F2();
}catch(...){
//Handle
}
}
但是,在发生错误时,我希望再次尝试该函数,如果出现错误,我想将错误传播回父类:
auto Interface()->bool
{
int error_count=0;
try{
try{
this->F1();
this->F2();
return true;
}catch(...){
if(error_count<1){this->F1(); this->F2();}
else{throw "Out of tries";}
}
}catch(...){
return false;
}
}
是否使用嵌套的 try/catch 块?这是最好的方法吗?
【问题讨论】:
标签: c++ error-handling try-catch