3

I have a "wrapper library" in C++/CLI that talks to an unmanaged third-party library to make the functionality available in my .net projects. Some functions of the unmanaged library throw exceptions which inherit from std::exception. I want to re-throw them as meaningful exceptions in .net space. Currently, I do this with every unmanaged call:

try {
    myThirdPartyObject -> doUnmanagedStuff();
} catch(std::exception e) {
    throw gcnew InvalidOperationException(gcnew String(e.what()));
}

However this does neither preserve the call stack nor the original exception. If I just let the exception bubble up, I only get it as a SEHException with the message "An external component has thrown an exception".

Is this possible and if yes, how is it done?

rabejens
  • 7,594
  • 11
  • 56
  • 104
  • A little bit off topic, but you should catch the C++ exceptions by reference (`catch (std::exception& e)`) – Stephan Oct 08 '15 at 13:09
  • Indeed, to avoid slicing. "const" reference is even better (`catch (const std::exception& e))` – Sebacote Oct 08 '15 at 13:12
  • @SébastienCôté: Why is catching a const reference better? You will never get a const exception in a `catch` block. And you could not call any non-const functions or modify the exception before rethrowing. – Stephan Oct 08 '15 at 13:32
  • @Stephan See "http://stackoverflow.com/questions/2145147/why-catch-an-exception-as-reference-to-const" and "http://stackoverflow.com/questions/7581654/about-catching-exception-good-practices" – Sebacote Oct 08 '15 at 13:41
  • You don't get a stack trace or anything but exception::what() in a native C++ program either. That does not get magically better when you rethrow it. – Hans Passant Oct 08 '15 at 15:03

1 Answers1

2

I don't think this is feasible. Unlike managed code, Standard C++ is not backed by a runtime engine that takes care of those things for you. There are ways to capture the stack trace, but they are platform-specific (see the Windows way here); and anyway, the stack information should be captured and attached to the exception object before throwing it, which in your case is not an option since you are calling into a third-party library I assume you cannot modify.

Sebacote
  • 690
  • 6
  • 17