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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T05:51:18+00:00 2026-05-14T05:51:18+00:00

I’m convinced at this point that I should be creating subclasses of std::exception for

  • 0

I’m convinced at this point that I should be creating subclasses of std::exception for all my exception throwing needs. Now I’m looking at how to override the what method.

The situation that I’m facing, it would be really handy if the string what returns be dynamic. Some pieces of code parse an XML file for example, and adding a position or line number to the error message is useful to me.

I am trying to follow the Boost Exception handling guidelines.

What I’d like to know:

  • what returns a const char *, which implies any catcher is likely not going to free the string. So I need some other place to store the result, but where would that be? (I need thread-safety.)

  • what also includes throw() in its signature. While I can prevent my what from throwing anything, it seems to me that this method really isn’t intended for anything too dynamic. If what is not the right place, then where should I be doing this instead?


From the answers I’ve gotten so far, it looks like the only way to accomplish this is by storing the string in the exception. The Boost guidelines recommend against this, which is confusing to me, because std::runtime_error does just that.

Even if I were to use a C-string, I’d have to use a statically sized buffer, or do memory management which can fail too. (I’m wondering if this is actually the only thing that can go wrong in std::string‘s copy-constructor. That would mean I won’t gain anything using dynamically allocated C-strings.)

Is there any other option left?

  • 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-14T05:51:18+00:00Added an answer on May 14, 2026 at 5:51 am

    My exception classes generally don’t have anything but the constructor and look along these lines:

    class MyEx: public std::runtime_error 
    {
    public: 
        MyEx(const std::string& msg, int line): 
            std::runtime_error(msg + " on line " + boost::lexical_cast<string>(line)) 
        {} 
    }; 
    

    An arbitrary example, but it is the base class that handles managing the what() message.

    But if you want to, you can also only assign the base part of the exception object, after you’ve put together the message in the constructor body.

    #include <stdexcept>
    #include <string>
    #include <sstream>
    
    class MyEx: public std::runtime_error
    {
    public:
        MyEx(const std::string& msg, int line):
            std::runtime_error("")
        {
            std::stringstream ss;
            ss << msg << " on line " << line;
            static_cast<std::runtime_error&>(*this) = std::runtime_error(ss.str());
        }
    };
    
    #include <iostream>
    int main()
    {
        try {
            throw MyEx("Evil code", __LINE__);
        }
        catch (const std::exception& e) {
            std::cout << e.what() << '\n';
        }
    }
    

    However, regarding the boost’s guidelines, perhaps you should pay attention to the point that numeric data (positions and lines) might best be made available as numbers through other methods. The guidelines say to worry less about the what() message.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I know there's a lot of other questions out there that deal with this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
I have this code to decode numeric html entities to the UTF8 equivalent character.

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.