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 7738415
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T08:15:30+00:00 2026-06-01T08:15:30+00:00

When I use boost::copy_exception to copy an exception to an exception_ptr , I lose

  • 0

When I use boost::copy_exception to copy an exception to an exception_ptr, I lose type information. Take a look at the following code:

try {
    throw std::runtime_error("something");
} catch (exception& e) {
    ptr = boost::copy_exception(e);
}
if (ptr) {
    try {
        boost::rethrow_exception(ptr);
    } catch (std::exception& e) {
        cout << e.what() << endl;
        cout << boost::diagnostic_information(e) << endl;
    }
}

From this, I get the following output:

N5boost16exception_detail10clone_implISt9exceptionEE
Dynamic exception type: boost::exception_detail::clone_impl<std::exception>
std::exception::what: N5boost16exception_detail10clone_implISt9exceptionEE

So basically boost::copy_exception statically copied the argument it got.

This problem is solved if I throw my exception with boost::enable_current_exception instead, like this.

try {
    throw boost::enable_current_exception(std::runtime_error("something"));
} catch (...) {
    ptr = boost::current_exception();
}
if (ptr) {
    try {
        boost::rethrow_exception(ptr);
    } catch (std::exception& e) {
        cout << e.what() << endl;
        cout << boost::diagnostic_information(e) << endl;
    }
}

The problem with this is that sometimes the exceptions are thrown by a library that does not use boost::enable_current_exception. In this case, is there any way to put the exception into an exception_ptr apart from catching each kind of possible exception one by one and use boost::copy_exception on each one?

  • 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-06-01T08:15:32+00:00Added an answer on June 1, 2026 at 8:15 am

    This is by design, and your analysis is correct: the static type is used, not the dynamic type. In fact, to avoid this surprise, boost::copy_exception became std::make_exception_ptr during the process that led to C++11. That way, it’s clearer that current_exception (whether the Boost version or the C++11 one) is the correct thing to use to correctly capture, well, the current exception. I highly recommend using BOOST_THROW_EXCEPTION, or at least boost::throw_exception, when it comes to using Boost.Exception-enabled exceptions in your code.

    When it comes to third-party code, there is no solution other than the one you already mentioned, or some other moral equivalent (like dynamic_cast-ing to the different leaf classes that make up the exception type hierarchy or hierarchies, or typeid abuse).

    In this respect exception handling is the same as working with one or more hierarchy of polymorphic types, and the operation you’re attempting in this case is a virtual copy, also known as cloning: either your code is intrusively designed to support that (as is the case when using e.g. BOOST_THROW_EXCEPTION(e); instead of throw e;), or you’re going to painfully walk the inheritance tree. Note that you can at least refactor that pain into just one site, such that you’d end up with e.g.

    try {
        third_party_api();
    } catch(...) {
        ptr = catch_exceptions_from_api();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The following code throws an exception terminate called after throwing an instance of 'boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::bad_function_call>
I'm trying to use boost::exception and have condensed my prototype code down to the
I use RCF with boost.serialization (why use RCF's copy when we already use the
I wanted to use boost::thread in my program, but get the following compiler error
I'm trying to use boost::filesystem to copy files and folders (just like a standard
I am making extensive use of boost:shared_ptr in my code. In fact, most of
I have recently started using boost::exception. Now I would like to use boost::errinfo_nested_exception to
I'm trying to use Boost::bind and std::copy to print out the values in a
I use boost::shared_ptr in my application in C++. The memory problem is really serious,
I'm trying to use boost::signal to implement a callback mechanism, and I'm getting a

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.