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?