Search code examples
c++unixexceptionshared-librariesdlopen

dlopen on library with static member that throws exception in constructor - results in Abort


I am trying to load a dynamic library using dlopen function. This library contains a static object, which throws an exception in its constructor. I have a "try-catch(...)" block around the dlopen call, but it doesn't catch the exception, and I just see "Abort" printed.

How am I able to catch this exception?


Solution

  • Short Answer: You Can't

    Thinking about it again.
    The original statements holds, but you must also remember that dlopen() is a C library function. C does not support exceptions. Thus throwing an exception that crosses from C++ code to C ( Your global object back upto dlopen() ) code will also cause application termination.

    See here:Why destructor is not called on exception?

    These are the situations under which throwing an exception will terminate the application. Your specific situation is covered by:

    An exception escapes the constructor/destructor of a non local static (ie global)