Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to catch unmanaged C++ exception in managed C++

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

like image 306
Arne Lund Avatar asked Jul 27 '11 19:07

Arne Lund


People also ask

Can you catch exceptions in C?

C itself doesn't support exceptions but you can simulate them to a degree with setjmp and longjmp calls.

Is C++ managed or unmanaged code?

So, by that definition all code compiled by traditional C/C++ compilers is 'unmanaged code'. Also, since it compiles to machine code and not an intermediate language it is non-portable.

What is exception handling C?

Exception handling is a mechanism that separates code that detects and handles exceptional circumstances from the rest of your program. Note that an exceptional circumstance is not necessarily an error. When a function detects an exceptional situation, you represent this with an object.


1 Answers

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();
}
like image 67
ildjarn Avatar answered Oct 05 '22 23:10

ildjarn