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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T13:50:28+00:00 2026-06-11T13:50:28+00:00

#include <boost/exception/all.hpp> #include <iostream> struct myexception : virtual boost::exception, virtual std::exception {}; typedef boost::error_info<struct

  • 0
#include <boost/exception/all.hpp>
#include <iostream>

struct myexception : virtual boost::exception, virtual std::exception {};
typedef boost::error_info<struct tag_info, std::string> info;

void main()
{
    try
    {
        BOOST_THROW_EXCEPTION(myexception()
            << info("1")
            << info("2") );
    }
    catch(const myexception& e)
    {
        std::cout << boost::diagnostic_information(e) << std::endl;
    }
}

This will output

[struct tag_info *] = 2

I understand why this is the case, but would rather have it output

[struct tag_info *] = 1
[struct tag_info *] = 2

I could, of course, typedef info as boost::error_info<struct tag_info, std::vector<std::string> > and then accumulate all infos in a std::vector before shifting it into the exception, but that has two downsides:
a) It involves copying of a std::vector
b) I need to build up the vector before throwing, i.e. I cannot simply use the shift operator to add more infos.

Thus I’m now looking for better solutions for adding several infos of the same error_info-type to an exception.

EDIT :
I tried to do as Josh Kelley suggested in his comment below and overload operator <<:

#include <boost/exception/all.hpp>
#include <iostream>
#include <vector>

typedef boost::error_info<struct tag_info, std::string> info;
typedef boost::error_info<struct tag_multiple_infos, std::vector<std::string> > multiple_infos;

struct myexception : virtual boost::exception, virtual std::exception
{
    myexception& operator<< (const info& rhs)
    {
        std::vector<std::string>* pinfos = boost::get_error_info<multiple_infos, myexception>(*this);
        if (pinfos != NULL)
        {
            pinfos->push_back(rhs.value());
        }
        else
        {
            std::vector<std::string> infos;
            infos.push_back(rhs.value());
            *this << multiple_infos(infos);
        }
        return *this;
    }
};

std::string to_string(const multiple_infos& info)
{
    std::ostringstream oss;
    std::for_each(info.value().begin(), info.value().end(),
        [&oss](const std::string& str) { oss << str << ' '; });
    return oss.str();
}

void main()
{
    try
    {
        BOOST_THROW_EXCEPTION(myexception()
            << info("1")
            << info("2") );
    }
    catch(const myexception& e)
    {
        std::cout << boost::diagnostic_information(e) << std::endl;
    }
}

That will output

[struct tag_multiple_infos *] = 1 2

That’s neat, but I like Pyotrs answer better, because it appears more natural to me and requires less code. However, if I wanted to add the infos across multiple catch sites1, then this solution would be more suitable, because I don’t need to know how many infos I have already added.

1 = I.e. shift infos into an exception, throw it, catch it somewhere else, shift more infos into it, then rethrow.

  • 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-11T13:50:30+00:00Added an answer on June 11, 2026 at 1:50 pm

    Just use two tags:

    struct tag_info1;
    struct tag_info2;
    typedef boost::error_info<tag_info1, std::string> info1;
    typedef boost::error_info<tag_info2, std::string> info2;
    

    Use like this:

        BOOST_THROW_EXCEPTION(myexception()
            << info1("1")
            << info2("2") );
    

    If you want more infos, use template:

    template <unsigned N>
    struct tag_info {};
    
    template <unsigned N>
    struct Nth {
      typedef boost::error_info<tag_info<N>, std::string> info;
    };
    
        BOOST_THROW_EXCEPTION(myexception()
            << Nth<1>::info("1")
            << Nth<2>::info("2") );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

#include <boost/ptr_container/ptr_vector.hpp> #include <iostream> using namespace std; using namespace boost; struct A { ~A()
#include <boost/regex.hpp> #include <string> #include <iostream> using namespace boost; static const regex regexp( std::vector<
#include <boost/regex.hpp> #include <string> #include <iostream> using namespace boost; static const regex regexp( std::vector<
The following code: #include <boost/variant.hpp> #include <iostream> #include <string> struct A { A() {
Here is the code: #include <iostream> #include <string> #include <map> #include <stdexcept> #include <boost/ptr_container/ptr_vector.hpp>
I have the following code: #include <iostream> #include boost/shared_ptr.hpp using boost::shared_ptr; class Base {
// BOOST Includes #include <boost/assign.hpp> // Boost::Assign #include <boost/assign/list_of.hpp> // Boost::Assign::List_Of #include <boost/assign/std/map.hpp> //
#include <iostream> #include <algorithm> #include <vector> #include <boost/array.hpp> #include <boost/bind.hpp> int main() { boost::array<int,
I have the following code: #include <boost/shared_ptr.hpp> struct Foo { int a; }; static
MinGw 4.7.0 Boost 1.47 code: #include <iostream> #include <boost/thread.hpp> int main(int argc, char *argv[])

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.