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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T07:37:49+00:00 2026-06-08T07:37:49+00:00

Why does std::runtime_error::what() return const char* instead of std::string const& ? In many cases

  • 0

Why does std::runtime_error::what() return const char* instead of std::string const& ?
In many cases a direct return of a reference to the embedded string would be handy and it could avoid some overhead. So what is the rationale for not returning e.g. a const reference to the internal string in the first place and not offering an overloaded function ? I guess it goes along with a string ctor being able to throw an exception as well but I do not see the risk of returning a string reference.

  • 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-08T07:37:51+00:00Added an answer on June 8, 2026 at 7:37 am

    std::runtime_error inherits from std::exception which defines virtual const char* what() const throw(); so the simplest response is that it’s an overload of the function and that you can be sure that any standard exception defines it in such way. It may (depending on the implementation) be possible to return the std::string, but it would be inconsistent with the rest of standard library.

    I think the reason why the what() returns const char* is that you can avoid any operation that can possibly fail (especially that can throw exception). Consider the following code, which shouldn’t fail

    virtual const char* what() const throw() {
        return "An error has occured";
    }
    

    However in the following code the allocation of std::string may fail, throwing an exception:

    std::string what() const throw() {
        return std::string("An error has occured");
    }
    

    If the string’s constructor threw here, the application would most likely crash no matter what, because the function specifies throw().

    Using std::string inside an exception introduce the need to allocate memory which may not be possible (note that std::bad_alloc inherits from std::exception too).

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

Sidebar

Related Questions

I use boost::asio::buffer to send a message using void Send(const std::string& messageData) { socket.async_write(boost::asio::buffer(messageData),
What advantages does QString offer over std::string? Can a normal std::string store unicode characters
std::string x(x); This crashes very badly on my compiler. Does this mean I should
#include<iostream> #include<string.h> using namespace std; char * reverse (char *); int main() { char
I wonder if this line creates a dangling pointer: string arg=derp; throw std::runtime_error(Unknown argument
std::string line; This throws std::runtime_error what(): Memory exhausted : regex_it = boost::sregex_iterator(line.begin(), line.end(), re);
std::exception requires that its constructor be throw() . Yet std::runtime_error accepts a std::string as
std::string sAttr(); sAttr = sAttr+VAL_TAG_OPEN+sVal->c_str()+VAL_TAG_CLOSE; else where in the code I have defined const
Does std::map move around already inserted values when inserting new data ?
Possible Duplicate: C++ Vector Pointers to Objects Does std::list::remove method call destructor of each

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.