tags:

views:

36

answers:

1

I have a C# application which calls a function in a C++ dll. This function can throw various exceptions which inherits std::exception. I currently catch these exceptions like this:

try
{
    //Call to C++ dll
}
catch (System.Exception exception)
{
    //Some error handling code
}

My first question is will this code catch all std::exception? My second question is how can I retrieve the std::exception::what string, if I examine exception.Message I only get "External component has thrown an exception".

EDIT: The function in question is in a non managed C++ dll, and imported like this in the C# class:

[DllImport("SomeDLL.dll")]
public extern static void SomeFunction();
A: 

Call how? The CLR doesn't really "get" C++ exception handling. If you call the C++ code through COM, add a layer that catches the std::exception and wrap it with a HRESULT/IErrorInfo. If you call it through managed C++, add a layer that wraps it in a managed System.Exception etc.

Mattias S
I've updated my question
Andreas Brinck
The same principle applies to PInvoke. It's easier to find some other protocol for error reporting. Using the return values, output parameters and/or Windows' SetLastError function for example.
Mattias S