3 回答
TA贡献2012条经验 获得超12个赞
#include <iostream>class Bad{
public:
// Added the noexcept(false) so the code keeps its original meaning.
// Post C++11 destructors are by default `noexcept(true)` and
// this will (by default) call terminate if an exception is
// escapes the destructor.
//
// But this example is designed to show that terminate is called
// if two exceptions are propagating at the same time.
~Bad() noexcept(false)
{
throw 1;
}};class Bad2{
public:
~Bad2()
{
throw 1;
}};int main(int argc, char* argv[]){
try
{
Bad bad;
}
catch(...)
{
std::cout << "Print This\n";
}
try
{
if (argc > 3)
{
Bad bad; // This destructor will throw an exception that escapes (see above)
throw 2; // But having two exceptions propagating at the
// same time causes terminate to be called.
}
else
{
Bad2 bad; // The exception in this destructor will
// cause terminate to be called.
}
}
catch(...)
{
std::cout << "Never print this\n";
}}例如:
编辑:
TA贡献1772条经验 获得超6个赞
- 3 回答
- 0 关注
- 818 浏览
添加回答
举报
