-1

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 , std::exception::what, <=> virtual const char* what() const noexcept;

from this example givin in c++ reference : what_example, i can understand the usage but i have a few questions:

// exception::what
#include <iostream>       // std::cout
#include <exception>      // std::exception

struct ooops : std::exception {
  const char* what() const noexcept {return "Ooops!\n";}
};

int main () {
  try {
      throw ooops();
  } catch (std::exception& ex) {
      std::cout << ex.what();
  }
  return 0;
}
  1. in c++98 the what() was : virtual const char* what() const throw();, and in c++11, it becomes virtual const char* what() const noexcept;. what is the noexcept at the end? did it bring something new?
  2. why should i use what() it at all? i can emplement my own tostring method in my class exception and call it instead!
  3. in the return value of what(), see below, guaranteed to be valid at least until...or until a non-const member function of the exception object is called. what is the meaning of or until a non-const member function of the exception object is called, could some one explain with example and why is that ?

what() return value

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 or until a non-const member function of the exception object is called.

thanks.

Adam
  • 2,820
  • 1
  • 13
  • 33
  • 3
    Please don't ask multiple questions in one. Especially when they can each be covered by their own SO post. The first one is a duplicate of https://stackoverflow.com/questions/12833241/difference-between-c03-throw-specifier-c11-noexcept – StoryTeller - Unslander Monica Jul 04 '20 at 23:27
  • @StoryTeller-UnslanderMonica-maybe in the link you have attached they have a partial answer, but this is still not answer my question. – Adam Jul 05 '20 at 00:00
  • This might be helpful: https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#e30-dont-use-exception-specifications – Marek R Feb 06 '23 at 11:07

3 Answers3

5
  1. what is the noexcept at the end?

It is a new specifier introduced in C++11. In very short, it means that the function will not throw an exception. noexcept has same meaning as throw().

did it bring something new?

noexcept is an improvement over the old throw specifier, which has since been deprecated (C++11) and then removed (C++20) from the language. It accepts a boolean expression that determines whether the function is noexcept or potentially throwing. This is useful in generic template programming because some instances of a template may be potentially throwing while others might not be.

  1. why should i use what() it at all? i can emplement my own tostring method in my class exception and call it instead!

Because you may be using functions that are not written by you, and therefore will not throw your exception class. For example, some standard functions will in some cases throw an exception, and all standard exceptions derive from std::exception. In such case, the only way to access the error message is through the what member function.

Same applies when other people call your function. They might not want to / or need to know about your special exception class, but they can still catch it and print the message if you inherit std::exception.

  1. what is the meaning of or until a non-const member function of the exception object is called

The meaning is literal. If you call what on an exception object derived from std::exception, and store the returned pointer, and then call a non-const member function of that exception object then the stored pointer will be invalid.

Any attempt to indirect through an invalid pointer such as attempting to print the exception message will result in undefined behaviour.

eerorika
  • 232,697
  • 12
  • 197
  • 326
0

For your first question check out StoryTeller's comment. As for the second point:

why should i use what() it at all? i can emplement my 
own tostring method in my class exception and call it instead!

The first rule of programming is do not reinvent the wheel, if there is a toString function or another function in the STL library that meets your needs, use it. Do not invent your own and try to debug it.

Yunfei Chen
  • 630
  • 1
  • 8
  • 20
-1

As an addition to the third question:

Sometimes, one happens to be surprised how important it can be that the wheel is not reinvented; and sharing the same basic class can be a big deal.

Thus, there comes a std::cerr message showing the contents of what, when the program crashes because of a thrown exception.

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
  • 1
    This doesn't answer the question. Avoid patronising coments. – jabellcu Feb 09 '23 at 12:39
  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/33785977) – Toby Speight Feb 09 '23 at 17:45