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;
}
- in c++98 the what() was :
virtual const char* what() const throw();
, and in c++11, it becomesvirtual const char* what() const noexcept;
. what is thenoexcept
at the end? did it bring something new? - why should i use
what()
it at all? i can emplement my owntostring
method in my class exception and call it instead! - 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.
Aucun commentaire:
Enregistrer un commentaire