上面的问题可以归结为理解以下两个代码片段的行为。
示例 1: 无活动异常抛出
int main()
{
try{
throw;
}catch(...){
std::cout<<"caught"<<endl; //we never reach here
}
return 0;
}
如果您运行上面的代码,它会崩溃如下
terminate called without an active exception
Aborted (core dumped)
示例 2:抛出活动异常
int main()
{
try{
throw 7;
}catch(...){
std::cout<<"caught"<<endl; //will be caught
}
return 0;
}
运行它会产生可预测的输出
caught
如果生成代码的程序集 ( g++ -S option
)。您会注意到以下 cxx_abi 调用 throw vs throw 7
throw;
转换为call __cxa_rethrow
和
throw 7;
转换为call __cxa_throw
这是代码__cxa_throw
extern "C" void
__cxxabiv1::__cxa_throw (void *obj, std::type_info *tinfo,
void (_GLIBCXX_CDTOR_CALLABI *dest) (void *))
{
PROBE2 (throw, obj, tinfo);
__cxa_eh_globals *globals = __cxa_get_globals ();
globals->uncaughtExceptions += 1;
// code removed for brevity
//.......
// Below code throws an exception to be caught by caller
#ifdef _GLIBCXX_SJLJ_EXCEPTIONS
_Unwind_SjLj_RaiseException (&header->exc.unwindHeader);
#else
_Unwind_RaiseException (&header->exc.unwindHeader);
#endif
// Some sort of unwinding error. Note that terminate is a handler.
__cxa_begin_catch (&header->exc.unwindHeader);
std::terminate ();
}
因此,在 OP Codethrow 7;
中会被相应的捕获并被catch(...)
重新抛出throw;
这是代码__cxa__rethrow
extern "C" void
__cxxabiv1::__cxa_rethrow ()
{
__cxa_eh_globals *globals = __cxa_get_globals ();
__cxa_exception *header = globals->caughtExceptions; // We are not re
globals->uncaughtExceptions += 1;
// Watch for luser rethrowing with no active exception.
if (header)
{
// Code removed for brevity
// .....
// Below code rethrows the exception
#ifdef _GLIBCXX_SJLJ_EXCEPTIONS
_Unwind_SjLj_Resume_or_Rethrow (&header->unwindHeader);
#else
#if defined(_LIBUNWIND_STD_ABI)
_Unwind_RaiseException (&header->unwindHeader);
#else
_Unwind_Resume_or_Rethrow (&header->unwindHeader);
#endif
#endif
}
std::terminate ();
}
在这两种情况下,我们都可以看到std::terminate()
尚未从__cxx_*
. 被上面的 abi 抛出后,我们在代码中的以下位置。
请参阅cxx_abi以终止代码。
void
__cxxabiv1::__terminate (std::terminate_handler handler) throw ()
{
__try
{
handler (); // Our handler has thrown an int exception
std::abort ();
}
__catch(...) // Exception is caught here and process is aborted.
{ std::abort (); }
}
void
std::terminate () throw()
{
__terminate (get_terminate ());
}
概括
根据我的理解,从处理程序重新抛出异常会导致在__cxxabiv1::__terminate
. 它调用的地方abort()
。显然,std::terminate()
[from __cxa_rethrow] 方法没有出现,这就是为什么控件从未到达 std::cout << "got here!" << std::endl;
无限递归
如果我们将 terminate_handler 更改为以下内容会发生什么:
void i_throw()
{
std::cout << "i_throw()" << std::endl;
throw;
std::cout << "got here!" << std::endl;
std::abort();
}
要理解这一点,我们可以看看__cxa_rethrow()
上面提到的。
由于没有抛出活动异常,__cxa_rethrow()
因此最终会调用std::terminate()
,从而导致无限递归。