public member function
<exception>

std::exception::what

virtual const char* what() const throw();
virtual const char* what() const noexcept;
Get string identifying exception
Returns a null terminated character sequence that may be used to identify the exception.

The particular representation pointed by the returned value is implementation-defined.

As a virtual function, derived classes may redefine this function so that specific values are returned.

参数



返回值

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.

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// 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;
}

可能的输出
Ooops!


异常安全

无异常保证:此成员函数从不抛出异常。
This also applies to all derived classes within the C++ standard library.

另见