11
votes

I am developing a thin managed C++ wrapper over a large unmanaged C++ library, and a large C# library. I need to catch errors originating in that large unmanaged C++ library, and rethrow them as Clr exceptions. The unmanaged library throws instances of the following class:

Error::Error(const std::string& file, long line,
             const std::string& function,
             const std::string& message) {
    message_ = boost::shared_ptr<std::string>(new std::string(
                                  format(file, line, function, message)));
}

const char* Error::what() const throw () {
    return message_->c_str();
}

So far I have come up with this:

try{
// invoke some unmanaged code
}
catch(Object*)
{
throw gcnew System::Exception("something bad happened");
}

How do I extract the message from Error class and convert it to the Clr String class, so that I can pass it to gcnew System::Exception() constructor? If the unmanaged code throws something else, will my catch block catch it?

Edit: I am using catch(Object*) because that is recommended in MCDN

3
If instances of Error are being thrown, why are you catching Object*? And what is Object in this context anyway?ildjarn
@ildjarn: this Object* thing was advised on MSDNArne Lund
That's relevant to Managed Extensions for C++, not C++/CLI. Two different languages, and given you're using gcnew, you're using C++/CLI.ildjarn
@ArneLund: Also, that MSDN article doesn't recommend catching by Object*. The only thing it says about Object* is a warning that it doesn't work right.Mooing Duck

3 Answers

10
votes

Does the following not work for you?

try
{
    // invoke some unmanaged code
}
catch (Error const& err)
{
    throw gcnew System::Exception(gcnew System::String(err.what()));
}

Because this certainly works for me:

#pragma managed(push, off)
#include <string>

struct Error
{
    explicit Error(std::string const& message) : message_(message) { }
    char const* what() const throw() { return message_.c_str(); }

private:
    std::string message_;
};

void SomeFunc()
{
    throw Error("message goes here");
}

#pragma managed(pop)

int main()
{
    using namespace System;

    try
    {
        try
        {
            SomeFunc();
        }
        catch (Error const& err)
        {
            throw gcnew Exception(gcnew String(err.what()));
        }
    }
    catch (Exception^ ex)
    {
        Console::WriteLine(ex->ToString());
    }
    Console::ReadLine();
}
5
votes

I use

#include <exception>
#include <msclr\marshal.h>

using namespace System;
using namespace msclr::interop;

try
{
    ...
}

catch (const std::exception& e)
{
    throw gcnew Exception(marshal_as<String^>(e.what()));
}

catch (...)
{
    throw gcnew Exception("Unknown C++ exception");
}

You may want to put this into a pair of macros since they'll be used everywhere.

You can add a custom catch block with your Error class, but since it seems to derive from std::exception, the code I show you should be OK.

You could also want to catch more specifically std::invalid_argument and translate it into ArgumentException, etc. but I find this overkill.

2
votes

The only reliable way I've come up with to catch most unmanaged exceptions is catch (...) which won't give you any information to rethrow, but will prevent most crashes. There are still some exceptions that even catch (...) won't catch and will crash your application, even without a crash indicator (the app just disappears), like if a badly written 3rd party app uses SetJump/LongJump with the wrong error handling or thread protocols.

You could write a long series of catch blocks if you wanted to try to type many C++ exceptions, like:

catch (int i)
{
  // Rethrow managed with int data
}
catch (double d)
{
    // Rethrow managed with double data
}
... etc
catch (...)
{
    // Rethrow managed "I got a general exception" error
}