我有一个类,它的构造函数会抛出异常。我还有一个 catch 块来处理该异常。但是,我仍然看到异常被传播回调用者,即使它已被处理。我知道,应该有一个异常通知调用者构造失败。但在这种情况下,它(重新抛出)是如何发生的?
class Test
{
public:
Test()
try
{
std::cout << "in child constructor" << std::endl;
throw 3.2;
}
catch(int &e)
{
std::cout << "in int catch: " << e << std::endl;
}
catch (double &e)
{
std::cout << "in double catch: " << e << std::endl;
}
};
int main (void)
{
try
{
Test obj;
}
catch (int &e)
{
std::cout << "in main int catch: " << e << std::endl;
}
catch (double &e)
{
std::cout << "in main double catch: " << e << std::endl;
}
return 0;
}
我得到的输出是
in child constructor
in double catch: 3.2
in main double catch: 3.2