6
votes

My project contains shared library and exe client. I found that my own exception class thrown from the library is not caught by client catch block, and program terminates with "terminate called after throwing an instance of ..." message. Continuing to play with the project, I found that any exception of any type is not caught. For example, this catch doesn't work:

    try
    {
        m_pSerialPort = new boost::asio::serial_port(m_IoService, "non-existing-port");
    }
    catch(const boost::system::system_error& e)
    {
        // ...
    }

Error message:

terminate called after throwing an instance of 
'boost::exception_detail::clone_impl
<boost::exception_detail::error_info_injector
<boost::system::system_error> >'
  what():  No such file or directory

GCC version is 4.4.1, Linux OS. The same code is running successfully in Windows, MSVC. What reason can prevent GCC program to catch exceptions properly?

1
Could you kindly post the Makefile so we can see what arguments you're passing to gcc? You can pass arguments which disable exceptions.Conspicuous Compiler
Additional information: when I add this code: try {throw 1;}catch(int){} to the client executable, everything is OK. But the same code in the shared library gives: terminate called after throwing an instance of 'int'Alex F
Conspicuous Compiler: technical question. How can I post Makefile here? Comment is restricted by length and doesn't allow any formatting.Alex F
In case you're seeing this behavior when using threads, check my similar issue here: stackoverflow.com/questions/1377833/…. Might be a long shot, but worth verifyinglaura
After changing -static switch to -static-libgcc in the shared library program seems to be working. I don't understand what is side effect of this change. Is my library still statically linked to third-party library? Anyway, this is the direction.Alex F

1 Answers

9
votes

Both the client .exe and the shared library should to be linked with libgcc in order to throw across shared library boundaries. Per the GCC manual:

... if a library or main executable is supposed to throw or catch exceptions, you must link it using the G++ or GCJ driver, as appropriate for the languages used in the program, or using the option -shared-libgcc, such that it is linked with the shared libgcc.