1

I searched but I don't find a solution for my answer. My problem is the following : one of my library throw a ns::xml::C_Exception when it does a check for a type. In another library, a class read a XML file for getting the configuration. And in the exec, I've got my try-catch with the correct type. But the exception isn't catch.

I checked the throw : I throw by value and :

try {
    throw ns::xml::C_Exception ("mesg");
}
catch (const ns::xml::C_Exception& ex)
{
    cout << ex.what () << endl;
}

Works.

If I use directly the check in the exec, this is the same as above, the exception is catched. But, when the C_Configuration throw an exception, the catch doesn't work (and even the catch (...) doesn't work).

For every try-catch block I used, this is the console output :

terminate called after throwing an instance of 'ns::xml::C_Exception'
  what():  mesg

I also checked all the functions used : they all have the throw (ns::xml::C_Exception) modifier.

C_Exception inherit directly from std::exception.

For testing, the C_Configuration always throws an exception :

bool
C_Configuration::load (string file)
{

    // Useless code here

    throw xml::C_Exception ("mesg");

    // Useless code here
}

// In the header file :
bool
C_Configuration::reload (void)
{ return load (_file); }

And this is my main :

int
main (int argc, char** argv)
{
    try {
        C_Configuration c("test.xml");
        c.reload ();
    }
    catch (const std::exception& ex)
    {
        cout << ex.what () << endl;
    }
    catch (...)
    {
        cout << "Oops" << endl;
    }

    // Useless code here.
    return 0;
}

The related files (there are in my GitHub repo, so you can see the full sources) :

Thanks for you help.

Edit :

Compiler flags :

  • For the exec : -std=c++11 (plus the includes)
  • For the libraries : -std=c++11 -fPIC (plus the includes)

Linker flags :

  • For the exec : Just the libraries and the library path.
  • For the libraries : -shared

EDIT : @ZanLynx said the problem comes from differents typeid between the two libraries. I merged the libraries into only one, but still the exception isn't caught.

  • Can you show where exactly in your code this issues occurs? – NathanOliver Aug 09 '16 at 15:44
  • Please include compiler/linker details including flags used. – Brian Cain Aug 09 '16 at 15:44
  • 2
    Exception specifications are deprecated (since C++11 I think) and have always traditionally been poorly supported by compilers. If a function can throw an exception, then it is necessary and sufficient to simply not mark it as `noexcept`. – KABoissonneault Aug 09 '16 at 15:47
  • As @KABoissonneault has already said exception specifications are deprecated. Maybe one of your functions throws an exception on different type that what is specified and the `terminate()` function is called. Anyway more details about where the error is generated will make the diagnostic easier! – BiagioF Aug 09 '16 at 15:53
  • @BiagioFesta : Okay, but even if it's the wrong type, the `catch (...)` should be used, and the program shouldn't terminate. – Nils 'Linkpy' Reid Aug 09 '16 at 16:03
  • @Nils'Linkpy'Reid nope. I refered to [this](http://en.cppreference.com/w/cpp/language/except_spec). "*If the function throws an exception of the type not listed in its exception specification, the function std::unexpected is called. The default function calls std::terminate, [...]*" – BiagioF Aug 09 '16 at 16:05
  • If this is a GCC compiler then types are identified by pointer values. This means that if you have a type in a library you need to make *sure* all of your code references the *same library*. If you have code that uses the type in a static library it won't match up with other code using that type. It can also break if wrapped in anonymous namespaces. – Zan Lynx Aug 09 '16 at 16:18
  • @ZanLynx : Every libraries is shared and I use GCC. By "you need to make sure all of your code references the same library.", what did you mean ? – Nils 'Linkpy' Reid Aug 09 '16 at 16:30
  • @Nils'Linkpy'Reid: Say that you have two shared libraries that both define the same C++ type. If they are built with internal linkage, each library will point to its *own* type. Exception handlers outside of the library won't be able to catch exceptions using those types because the pointers to typeinfo won't match. – Zan Lynx Aug 09 '16 at 17:07
  • @ZanLynx : But the C_Exception class is mostly inline, and only one method is in a .cpp file, which is in the XML library. Also in this case this problem can happen ? (BTW, what did you mean by "internal linkage" ? If the libraries are staticly linked ?) – Nils 'Linkpy' Reid Aug 09 '16 at 17:27
  • I think this come from inline functions (in the ns_xml library) which throws an exception. The ns_framework use these functions and this is why there are this problem of different TypeID. – Nils 'Linkpy' Reid Aug 10 '16 at 15:05

0 Answers0