Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exceptions are not caught in GCC program

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?

like image 961
Alex F Avatar asked Mar 11 '10 12:03

Alex F


People also ask

What is GCC exception?

The purpose of this Exception is to allow compilation of non-GPL (including proprietary) programs to use, in this way, the header files and runtime libraries covered by this Exception.

What if exception is not caught?

What happens if an exception is not caught? If an exception is not caught (with a catch block), the runtime system will abort the program (i.e. crash) and an exception message will print to the console. The message typically includes: name of exception type.

How do you handle exceptions without catching?

throws: The throws keyword is used for exception handling without try & catch block. It specifies the exceptions that a method can throw to the caller and does not handle itself.

Can you catch exceptions in C?

C itself doesn't support exceptions but you can simulate them to a degree with setjmp and longjmp calls.


1 Answers

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.

like image 184
karunski Avatar answered Oct 15 '22 21:10

karunski