tags:

views:

68

answers:

1

I have the following code in a c++/CLI library to catch unmanaged exceptions and rethrow them:

catch(const std::exception &e)
{
    String ^errorMessage = String::Format(L"Parser threw exception: {0}", gcnew String(e.what()));
    throw gcnew ApplicationException(errorMessage);
}

Is this the best way? I seem to be losing a lot of information this way.

+1  A: 

I assume you mean that you are throwing away the derived type, and any data it holds. There isn't much you can do about that as a generic solution. Of course, if you use library Foo, and it often throws FooError, you can catch (const FooError& e) and handle that specially.

You could also use RTTI to discover the runtime type of the exception and add that to the .Net exception. The name that pops out is somewhat ugly.

Marcelo Cantos
Thanks for your comment :)
DanDan