1.抛出异常后可能无法执行之后释放资源的语句
2.C++无法处理同时抛出的两个异常,通常异常发生时,c++的机制会调用已经构造对象的析构函数来释放资源,此时若析构函数本身也抛出异常,则前一个异常尚未处理,又有新的异常,会造成程序崩溃的问题。
默认析构函数都有加noexcept,也可以强制无视noexcept抛出异常
class EvilB : public B{
public:
//~EvilB(){}
~EvilB() noexcept(false) { throw std::string("error");}
};
void test(){
try{
EvilB b;
}catch(std::string const& e){
std::cout<<"catch you evil " << e << std::endl;
}
}
基类和派生类同时抛出异常则程序会崩,一个catch无法同时捕获两个异常;
class B : public A {
public:
B(){}
~B() noexcept(false) {
std::cout<<"byebye B\n";
throw std::string("error B");
}
private:
std::string m_value;
}
class EvilB : public B {
public:
//~EvilB(){}
~EvilB() noexcept(false) {throw std::string("error");}
};
void test(){
try{
EvilB b;
}catch(std::string const& e){
std::cout<<"catch you evil" << e << std::endl;
}
}
定义两个相同类同时捕获一个异常程序也会崩
class B : public A {
public:
B(){}
~B() noexcept(false) {std::cout<<"byebye B\n";}
private:
std::string m_value;
}
class EvilB : public B {
public:
//~EvilB(){}
~EvilB() noexcept(false) { throw std::string("error");}
};
void test(){
try{
EvilB b;
EvilB c;
}catch(std::string const& e) {
std::cout<<"catch you evil"<< e << std::endl;
}
}