title: Cannot catch exception from shared library in main.cpp
body: <p>I'm currently working on a game with a plugin based architecture. The executable consists mostly of a shared library loader and a couple of interface definitions. All the interesting stuff is happening in dynamic shared libraries which are loaded at start up.</p>
  
  <p>One of the library classes throws an exception under certain circumstances. I would expect to be able to catch this exception and do useful stuff with it but this is where it gets weird. See following simplified example code:</p>
  
  <p><strong>main.cpp</strong></p>
  
  <pre><code>int main()
  {
    try
    {
      Application app;
      app.loadPlugin();
      app.doStuffWithPlugin();
      return 0;
    }
    catch(const std::exception&amp; ex)
    {
       // Log exception
       return 1;
    }
  }
  </code></pre>
  
  <p><strong>Application.cpp</strong></p>
  
  <pre><code>...
  void doStuffWithPlugin()
  {
    plugin.doStuff();
  }
  ...
  </code></pre>
  
  <p><strong>Plugin.cpp</strong></p>
  
  <pre><code>...
  void doStuff()
  {
     throw exception_derived_from_runtime_error("Something is wrong");
  }
  ...
  </code></pre>
  
  <p>Plugin.cpp exists in a dynamic shared library which is successfully loaded and which has afterwards created an object of class Plugin. The exception_derived_from_runtime_error is defined in the application. There is no <code>throw()</code> or <code>noexcept</code>.</p>
  
  <p>I would expect to catch the exception_derived_from_runtime_error in main but that doesn't happen. Compiled with GCC 4.8 using C++11 the application crashes with <code>This application has requested the Runtime to terminate it in an unusual way.</code>.</p>
  
  <p>I replaced <code>catch(const std::exception&amp; ex)</code> with <code>catch(...)</code> but that didn't make any difference. The weird part is if i catch the exception in doStuffWithPlugin() it works. If i rethrow it using <code>throw;</code> it fails again but it can be caught if i use <code>throw ex;</code>:</p>
  
  <p><strong>Application.cpp</strong></p>
  
  <pre><code>void doStuffWithPlugin()
  {
    try
    {
        plugin.doStuff();
    }
    catch(const exception_derived_from_runtime_error&amp; ex)
    {
       // throw; &lt;- Not caught in main().
       // throw ex; &lt;- Caught in main().
    }
  }
  </code></pre>
  
  <p>Hopefully somebody has an idea. Thanks for every help you can give.</p>