【发布时间】:2017-04-22 09:32:05
【问题描述】:
我有一个嵌套的 try-catch 代码,如下所示:
void A()
{
try
{
//Code like A = string(NULL) that throws an exception
}
catch(std::exception& ex)
{
cout<<"in A : " << ex.what();
throw ex;
}
}
void B()
{
try
{
A();
}
catch(std::exception& ex)
{
cout<<"in B : " << ex.what();
}
}
运行后我得到了这个结果:
in A: basic_string::_M_construct null not valid
in B: std::exception
如您所见,ex.what() 在函数 A 中工作正常并告诉我正确的描述,但在 B 中 ex.what() 只告诉我 std::exception。为什么会这样?
我是否在函数 A 的 catch 子句中抛出了一些不同或错误的东西?如何抛出嵌套异常,以便在 B 中获得准确的异常描述?
【问题讨论】:
标签: c++ exception-handling try-catch