【问题标题】:c++ throw with try catch all always hits terminate in C++ 11 14 and 17c++ throw with try catch all always hits 在 C++ 11 14 和 17 中终止
【发布时间】:2020-09-03 20:30:01
【问题描述】:

我不断收到终止调用我使用 GCC 9.2 抛出的任何内容,即使它被捕获。

terminate called after throwing an instance of 'char const*'
terminate called recursively

我测试过 -std=c++17, -std=c++14, -std=c++11

示例测试:

#include <iostream>

int main()
{
  try
  {
      throw "not found";
  }
  catch(...)
  {
    std::cout << "test" << std::endl;
  }

  return 0;
}

如果我使用 Visual Studio 或多个在线编译器进行编译,它不会失败。 例子: https://repl.it/languages/cpp https://www.onlinegdb.com/online_c++_compiler

我也尝试过将 throw 放入一个函数并添加 noexcept(false),但这也失败了。示例:

#include <iostream>

void foo() noexcept(false)
{
    throw std::runtime_error( "test1" );
}

int main()
{
  try
  {
      foo();
  }
  catch(...)
  {
    std::cout << "test2" << std::endl;
  }

  return 0;
}

编辑:

系统信息:

我正在使用 9-2020-q2-update - arm-linux-none-gnueabihf。

基本上,设置是 Linux x86 作为我的主计算机,针对 ARM Cortex-A 处理器进行交叉编译。我正在测试的处理器是 Raspberry Pi 4 和 BeagleBone Black。

代码可以正确编译并且在目标处理器上运行良好,除非遇到异常。在这一点上,它会因任何抛出而终止。

我使用 Eclipse 作为 IDE,使用远程调试上传和单步执行任一目标处理器上的代码。

【问题讨论】:

  • 请添加有关您获得此结果的系统的详细信息。
  • 我已经尝试过repl.it 和 Godbolt,两者都完全按照应有的方式打印出 test。您的代码没有问题,但可能与您的编译器标志有关。
  • 是的,我也是这么想的。它必须是缺少编译器标志或其他东西。我试过-fexceptions,但这没有用。
  • @user2654735 你能复制它here吗?

标签: c++ try-catch terminate throw


【解决方案1】:

似乎有一个错误或异常处理不适用于 GCC 9.2 版(仅限 ARM?)编译器。

我尝试使用版本 8.3-2019.03 - arm-linux-gnueabihf - Linux x86 编译器,它们运行良好。除了编译开关之外,无需进行其他更改。

https://developer.arm.com/tools-and-software/open-source-software/developer-tools/gnu-toolchain/gnu-a/downloads

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-17
    • 2015-07-07
    • 2015-05-03
    相关资源
    最近更新 更多