Search code examples
c++try-catchterminatethrow

c++ throw with try catch all always hits terminate in C++ 11 14 and 17


I keep getting terminate called for anything I throw using GCC 9.2, even if it is caught.

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

I have tested -std=c++17, -std=c++14, -std=c++11

Example test:

#include <iostream>

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

  return 0;
}

It doesn't fail if i compile using visual studio or on multiple of the online compilers. example: https://repl.it/languages/cpp https://www.onlinegdb.com/online_c++_compiler

I have also tried putting the throw in a function and adding noexcept(false), but this fails as well. Example:

#include <iostream>

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

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

  return 0;
}

Edit:

System info:

I'm using 9-2020-q2-update - arm-linux-none-gnueabihf.

Basically, the setup is Linux x86 as my main computer, cross compiling for ARM Cortex-A processor. The processor i'm testing are Raspberry Pi 4 and BeagleBone Black.

The code compiles correctly and runs fine on the target processor, except when an exception is hit. At which point, it hits terminate for any throw.

I'm using Eclipse as the IDE, using remote debug to upload and step through the code on either of the target processors.


Solution

  • It seems there is a bug or exception handling isn't working on version 9.2 of GCC (ARM only?) compiler.

    I tried with version 8.3-2019.03 - arm-linux-gnueabihf - Linux x86 compiler and they are working just fine. No other changes were necessary, other than the compile switch.

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