The above question can be reduced to understanding the behavior of the following two pieces of code.
Example 1: a throw without an active exception
int main()
{
try{
throw;
}catch(...){
std::cout<<"caught"<<endl;
}
return 0;
}
If you run the above code, it will work as shown below
terminate called without an active exception
Aborted (core dumped)
2:
int main()
{
try{
throw 7;
}catch(...){
std::cout<<"caught"<<endl;
}
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;
#ifdef _GLIBCXX_SJLJ_EXCEPTIONS
_Unwind_SjLj_RaiseException (&header->exc.unwindHeader);
#else
_Unwind_RaiseException (&header->exc.unwindHeader);
#endif
__cxa_begin_catch (&header->exc.unwindHeader);
std::terminate ();
}
, OP- throw 7; catch(...) throw;
__cxa__rethrow
extern "C" void
__cxxabiv1::__cxa_rethrow ()
{
__cxa_eh_globals *globals = __cxa_get_globals ();
__cxa_exception *header = globals->caughtExceptions;
globals->uncaughtExceptions += 1;
if (header)
{
#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 ();
std::abort ();
}
__catch(...)
{ 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(), .