title: A mechanism to ensure exception propagation when mixing C and C++ code
body: <p>I'm not asking if it is safe for a C++ exception to propagate through C code, nor what happens when such thing occurs. I have read the following questions in SO(<a href="https://stackoverflow.com/questions/2101390/will-c-exceptions-safely-propagate-through-c-code">1</a>, <a href="https://stackoverflow.com/questions/6426835/what-happens-if-exception-gets-thrown-through-c-code">2</a>, <a href="https://stackoverflow.com/questions/4485398/exception-propagation-in-externally-linked-c-libraries">3</a>) and <a href="http://www.parashift.com/c++-faq-lite/mixing-c-and-cpp.html" rel="nofollow noreferrer">this FAQ</a>. I'm asking how to proceed to :</p>
  
  <ul>
  <li>Avoid leaking any C++ exception toward C code (this implies catching all exceptions in the C++ land before calling C code)</li>
  <li>Also be able to catch the exceptions outside the C code (in a higher C++ code).</li>
  </ul>
  
  <p>Let me illustrate my idea :</p>
  
  <p>Say <code>libfoo</code> is a C library, that I want to use in my <code>bar</code> C++ program. <code>libfoo</code> needs a callback function <code>foo_callback</code> that I must provide. The functions and methods used in my callback may throw an exception, so I wrote :</p>
  
  <pre><code>void my_callback(void)
  {
      try
      {
          // Do processing here.
      }
      catch(...)
      {
          // Catch anything to prevent an exception reaching C code.
          // Fortunately, libfoo provides a foo_error function to
          // signal errors and stop processing.
          foo_error() ;
      }
  }
  </code></pre>
  
  <p>And then I use my callback as shown below :</p>
  
  <pre><code>// The bar program.
  int main()
  {
      // Use libfoo function to set the desired callback
      foo_set_callback(&amp;my_callback) ;
  
      // Start processing. This libfoo function uses internally my_callback.
      foo_process() ;
  
      // Check for errors
      if( foo_ok() )
      {
          // Hurray ! 
      }
      else
      {
          // Something gone wrong.
          // Unfortunately, we lost the exception that caused the error :(
      }
  }
  </code></pre>
  
  <p>What I want is to be able to catch the exceptions thrown from <code>my_callback</code> in the <code>main</code> function, without having the exception propagating through <code>libfoo</code> (Yes, it's a sort of quantum exception experimenting <a href="http://en.wikipedia.org/wiki/Quantum_tunnelling" rel="nofollow noreferrer">quantum tunnelling</a> through C code). </p>
  
  <p>So the code I would love to use :</p>
  
  <pre><code>void my_callback(void)
  {
      try
      {
          // Do processing here.
      }
      catch(...)
      {
          // Catch anything to prevent an exception reaching C code.
          // We save the exception using (the magic) ExceptionHolder.
          ExceptionHolder::Hold() ;
  
          // Call foo_error function to signal errors and stop processing.
          foo_error() ;
      }
  }
  
  // The bar program.
  int main()
  {
      // Use libfoo function to set the desired callback
      foo_set_callback(&amp;my_callback) ;
  
      try
      {
          // Start processing. This libfoo function uses internally my_callback.
          foo_process() ;
  
          // Once gone out of the C land, release any hold exception.
          ExceptionHolder::Release() ;
      }
      catch(exception &amp; e)
      {
          // Something gone wrong.
          // Fortunately, we can handle it in some manner.
      }
      catch( /*something else */ )
      {
      }
      // ...
  }
  </code></pre>
  
  <p>Given the following constraints :</p>
  
  <ul>
  <li><code>libfoo</code> is source-closed, written in C and provided in compiled format from a vendor. Tests conducted on the library showed that exceptions cannot propagate through it. I have no access to the source files nor I can obtain a compiled version that supports exceptions.</li>
  <li>The callback function makes extensive use of C++ code that uses exceptions. All the error handling is built around the exception mechanism. In no way I can simply use the code that swallows all exceptions.</li>
  <li>No multithreading involved.</li>
  <li>No c++0x support.</li>
  </ul>
  
  <p>My questions :</p>
  
  <ul>
  <li>Is it already solved by some library or some C++ magic (like <code>boost</code>), or even c++0x ?</li>
  <li>If not, how can I write an ExceptionHolder that works with any exception type ? I'm confortable with C++ but I haven't found a way to write a reliable and easy to use ExceptionHolder that works with any exception type.</li>
  </ul>
  
  <p>Many thanks for any advice !</p>
  
  <hr>
  
  <p><em>EDIT : I added a response with a little implementation of the exception holding/releasing mechanism. All critics or propositions are welcome.</em></p>