Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6191113
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T02:42:22+00:00 2026-05-24T02:42:22+00:00

I am developing a thin managed C++ wrapper over a large unmanaged C++ library,

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T02:42:23+00:00Added an answer on May 24, 2026 at 2:42 am

    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();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Developing for iPhone, I have a collection of points that I need to make
Developing rails has become lots of fun over the year that I've done it,
Developing a project of mine I realize I have a need for some level
Developing a C# .NET 2.0 WinForm Application. Need the application to close and restart
I am developing a device that will continuously collect data from a PLC and
I am developing a Java web services application that is (mostly) to be used
Developing a Struts 2 app I run in a following problem. I need to
Developing an application for Android, i want to record data that will be usefull
I developing a program that draws in elements of the list (QListView). How do
Developing a site that requires monthly subscriptions via PayPal. If a buyer has an

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.