请看看演示代码:一些问题有关异常处理
class myError
{
const char* str;
public:
myError():str(NULL) {}
myError(const char* temp)
{
str = temp;
}
const char* what()
{
return str;
}
};
class ab
{
int x;
public:
ab() try :x(0)
{
throw myError("error occured in the constructor of class ab");
}
catch(myError& temp)
{
std::cout<<"Handler no. 1 of ab constructor"<<std::endl;
}
};
int main() try
{
ab bb;
cout << "Resumed execution!" << endl;
return 0;
}
catch(myError& temp)
{
std::cout<<"Handler below the main function"<<std::endl;
std::cout<<"And the error is :" <<temp.what();
}
我的问题:构造函数的
- 为什么只有功能try块的处理器和析构函数只rethows例外呢? ,
,当你简单地抛出内部构造函数的异常,其处理程序不会重新抛出的对象?即
Ctor::Ctor()
{
try{
throw Excep1();
}
catch(Excep1& temp) {
std::cout<<"Doesn't rethrows the exception object";
}
}
我想知道如何恢复控制权交还给
cout << "Resumed execution!" << endl;
,处理重新抛出对象后?为什么经常说,我们不应该把功能try块在析构函数的基类的?
的析构函数,你的意思析构函数? – MGZero
@ MGZero:是) –
我看不出在你的代码中重新抛出。你只是捕捉例外。 – VestniK