terminate called after throwing an instance of 'std::string'

14,913

Perhaps some or all modules were compiled with -fno-exceptions? See http://gcc.gnu.org/onlinedocs/libstdc++/manual/using_exceptions.html for details on how that changes exception behavior.

For example, the following short program displays "terminate called after throwing an instance of 'std::string'" if:

  • the module that contains foo() was compiled with -fno-exceptions, and
  • foo() calls something that throws an exception of type std::string (all other modules were compiled with -fexceptions)

    #include <string>
    #include <iostream>
    
    using namespace std;
    
    int foo();
    
    int main()
    {
        try {
            foo();
        }
        catch (string& s) {
            std::cout << "caught it: \"" << s << "\"" << endl;
        }
    
        return 0;
    }
    

Note that if I simply recompile foo.cpp with -fexceptions (g++'s default) and relink, the program displays:

caught it: "the string exception"

as expected.


Or perhaps some intermediate function had a throw specification that didn't list std::string?

For example, this program:

#include <string>
#include <iostream>

using namespace std;

int Hunc() throw(int); // can only throw int (?)

int main()
{
    try {
        Hunc();
    }
    catch (string& s) {
        std::cout << "caught it: \"" << s << "\"" << endl;
    }

    return 0;
}


int Hunc() throw(int)
{
    throw string("the string exception");
}

Also displays "terminate called after throwing an instance of 'std::string'". Both examples were tested on a Windows box with MinGW 4.5.1.

Share:
14,913
Admin
Author by

Admin

Updated on June 05, 2022

Comments

  • Admin
    Admin almost 2 years

    I have this binary thats crashing by throwing an exception of type std:string.

    Stack trace from a stripped binary:

    terminate called after throwing an instance of 'std::string'
    *** Aborted at 1309483487 (unix time) try "date -d @1309483487" if you are using GNU date ***
    PC: @       0x3fb0c30155 (unknown)
    *** SIGABRT (@0xd54) received by PID 3412 (TID 0x40d03940) from PID 3412; stack trace: ***
        @       0x3fb180de70 (unknown)
        @       0x3fb0c30155 (unknown)
        @       0x3fb0c31bf0 (unknown)
        @     0x2aaaaab80cc4 (unknown)
        @     0x2aaaaab7ee36 (unknown)
        @     0x2aaaaab7ee63 (unknown)
        @     0x2aaaaab7ef4a (unknown)
        @           0x4c2622 XYZ::connect()
        @           0x4c3e0f XYZ::refresh()
        @       0x3fb18062f7 (unknown)
        @       0x3fb0cd1e3d (unknown)
    

    Now the thing is, the refresh() function does try to catch std::string. It looks like:-

    bool XYZ::refresh() {
      try {
        connect();
      } catch (string& s) {
        return false;
      }
      return true;
    }
    

    Any idea why is it not getting caught? Or Am I reading the stack trace wrong?