title: what is std::exception::what() and why to use it?
body: <p>I came from c++98, and i am trying to make my way in to c++11 and so on. i came across the public member function , <code>std::exception::what</code>, &lt;=&gt; <code>virtual const char* what() const noexcept;</code></p>
  <p>from this example givin in c++ reference : <a href="http://www.cplusplus.com/reference/exception/exception/what/#:%7E:text=std%3A%3Aexception%3A%3Awhat&amp;text=The%20particular%20representation%20pointed%20by,that%20specific%20values%20are%20returned." rel="nofollow noreferrer">what_example</a>, i can understand the usage but i have a few questions:</p>
  <pre><code>// exception::what
  #include &lt;iostream&gt;       // std::cout
  #include &lt;exception&gt;      // std::exception
  
  struct ooops : std::exception {
    const char* what() const noexcept {return &quot;Ooops!\n&quot;;}
  };
  
  int main () {
    try {
        throw ooops();
    } catch (std::exception&amp; ex) {
        std::cout &lt;&lt; ex.what();
    }
    return 0;
  }
  </code></pre>
  <ol>
  <li>in c++98 the what() was : <code>virtual const char* what() const throw();</code>, and in c++11, it becomes <code>virtual const char* what() const noexcept;</code>. what is the <code>noexcept</code> at the end? did it bring something new?</li>
  <li>why should i use <code>what()</code> it at all? i can emplement my own <code>tostring</code> method in my class exception and call it instead!</li>
  <li>in the return value of what(), see below, guaranteed to be valid at least until...<strong>or until a non-const member function of the exception object is called</strong>. what is the meaning of <strong>or until a non-const member function of the exception object is called</strong>, could some one explain with example and why is that ?</li>
  </ol>
  <p><code>what() return value</code></p>
  <blockquote>
  <p>A pointer to a c-string with content related to the exception. This is
  guaranteed to be valid at least until the exception object from which
  it is obtained is destroyed <strong>or until a non-const member function of
  the exception object is called</strong>.</p>
  </blockquote>
  <p>thanks.</p>