终止处理程序可以抛出异常吗?

2024-05-25

以下程序的定义行为是什么(如果有)?

#include <iostream>
#include <exception>
#include <cstdlib>

void i_throw()
{
    std::cout << "i_throw()" << std::endl;
    // std::terminate() is noexcept so if the terminate handler throws...
    // then the terminate handler is called...
    // std::terminate is [[noreturn]] so don't return
    try
    {
        throw 7;
    }
    catch(...)
    {
        std::cout << "caught exception, re-throw()-ing" << std::endl;
        throw;
    }
    std::cout << "got here!" << std::endl;
    std::abort();
}

int main()
{
    std::set_terminate(i_throw);
    throw;
    std::terminate();
}

使用 gcc 和 clang 我得到以下输出:

i_throw()
caught exception, re-throw()-ing
Aborted (core dumped)

在前几条评论后编辑的示例。

(我不知道为什么我两个都有throw; and std::terminate();。我不想改变这个例子,所以就假装只有这两个之一。)


上面的问题可以归结为理解以下两个代码片段的行为。

样本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 与 throw 7

throw;被转换为call __cxa_rethrow

and

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代码中throw 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 https://github.com/gcc-mirror/gcc/blob/master/libstdc%2B%2B-v3/libsupc%2B%2B/eh_terminate.cc用于终止代码。

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 ());
}

Summary

根据我的理解,从处理程序重新抛出异常会导致捕获重新抛出的异常__cxxabiv1::__terminate。它在哪里调用abort()。显然,std::terminate()[来自 __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(),从而导致无限递归。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

终止处理程序可以抛出异常吗? 的相关文章

随机推荐