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

  • SEARCH
  • Home
  • 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 7986365
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T11:50:12+00:00 2026-06-04T11:50:12+00:00

I want to create a number of exception types which derive from std::runtime_error and

  • 0

I want to create a number of exception types which derive from std::runtime_error and I want them to have stringstream type functionality. I’ve therefore created an exception class which composes a std::stringstream and which derives from std::runtime_error:

template<typename T>
class error_stream : public std::runtime_error
{
public:
      error_stream() :
      std::runtime_error(""), ss(std::make_shared<std::basic_stringstream<T>> ())
      {
      }

      ~error_stream() throw()
      {
      }

      template <typename T>
      error_stream & operator << (const T & t)
      {
          *ss << t;
          return *this;
      }

      virtual const char * what() const throw()
      {
          get_str(s_, ss->str());
          return s_.c_str();
      }

protected:

    template <typename T>
    void get_str(std::basic_string<char> & s_, const std::basic_string<T> & str) const
    {
        s_ = str;
    }

    template<>
    void get_str(std::basic_string<char> & s_, const std::basic_string<wchar_t> & str) const
    {
        std::basic_string<char> ret(str.begin(), str.end());
        s_ = ret;
    }

protected:
    std::shared_ptr<std::basic_stringstream<T>> ss;
    mutable std::basic_string<char> s_;
};

And I’ve created a more specific exception type which in turn derives from this error_stream exception:

template<typename T>
class w32file_exception : public w32utils::error_stream<T> 
{
public:
    w32file_exception() : error_stream<T>() {}
};

However, I’ve encountered something I don’t understand here, because when I throw a w32file_exception I actually can only catch it as it’s parent error_stream. Can anyone see what I’m doing wrong?

    try
    {
        throw w32file_exception<char>() << "test";
    }
    catch ( w32file_exception<char> & e )
    {
        ASSERT_PASSED;
    }
    catch ( error_stream<char> & e )
    {
        std::cout << e.what() << std::endl;  // Why do I end up here?
    }
  • 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-04T11:50:13+00:00Added an answer on June 4, 2026 at 11:50 am

    What exactly does your throw look like? Are you using your operator<< before calling throw, like this:

    throw w32file_exception<T>() << "fooobar";
    

    Then the answer is, that your operator<< returns an error_stream and no w32file_exception and so the type of the thrown exception is error_stream.

    You could solve this problem this way:

    template<typename T, typename DERIVED>
        class error_stream : public std::runtime_error
    {
    public:
        // ...
        template <typename U>
            DERIVED & operator << (const T & t)
        {
            *ss << t;
            return static_cast<DERIVED&>(*this);
        }
        // ...
    };
    

    But then you loose the ability to catch every error_stream exception because it is a new Type for every DERIVED type.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to create dynamic number of thread which is depends on the database
I want to create a report which aggregates the number of activities per customer
i want to create a NFC SmartPoster which dial a number with Action Record
I have a table containing a column of type Number create table tmp (
I want to create certain number of threads in my program where the number
I want to create a web page where a registered user picks a number
i want create image animation , i have 50 images with png format now
I want create module which update list of usb devices automatically (not only mass
On my website I want the user to have the ability to login/logout from
I have viewmodel which i am contructing in a Create get action, public class

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.