【问题标题】:Can a terminate handler throw an exception?终止处理程序可以抛出异常吗?
【发布时间】:2015-02-27 03:43:50
【问题描述】:

以下程序的定义行为是什么?

#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)

在前几个 cmets 后编辑的示例。

(我不知道为什么我同时拥有throw; 和std::terminate();。我不想更改示例,所以就假装只有这两者之一。)

【问题讨论】:

  • 这个程序导致无限递归。与任何其他无限递归一样,它最终会由于耗尽实现的资源限制(1.4/(2.1))而异常结束。请注意,这里没有抛出异常,terminate 的noexcept 规范是无关紧要的。在没有操作数的情况下执行throw;,同时没有处理异常调用terminate() (5.17/4)
  • @IgorTandetnik 我想我在您的评论中看到了答案。您能否澄清一下没有抛出异常是什么意思?
  • 终止处理程序需要终止程序的执行。你的没有。
  • @T.C.但它会。这是否意味着有效的终止处理程序不能允许异常冒泡?
  • @Praxeolitic 1) 代码中有一条注释表明 OP 认为有一个异常被抛出,并且对 terminate() 的调用是由该异常向上和向外传播引起的 terminate 和后者是noexcept。但是throw; 不带参数只会在当前正在处理异常时抛出异常;否则,它只是直接调用terminate。后一种情况实际上发生在示例中。

标签: c++ exception language-lawyer terminate-handler


【解决方案1】:

上面的问题可以归结为理解下面两个代码sn-ps的行为。

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

因此,在操作码中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。

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 &lt;&lt; "got here!" &lt;&lt; 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(),从而导致无限递归。

【讨论】:

    猜你喜欢
    • 2011-12-12
    • 2011-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-13
    • 1970-01-01
    • 1970-01-01
    • 2012-02-09
    相关资源
    最近更新 更多