C++ Exception 库 - what
描述
它用于获取字符串识别异常。
声明
以下是 std::what 的声明。
virtual const char* what() const throw();
C++11
virtual const char* what() const noexcept;
参数
none
返回值
它返回一个以空字符结尾的字符序列,可用于识别异常。
异常
No-throw guarantee − 没有成员抛出异常。
示例
在下面的 std::what 示例中。
#include <iostream>
#include <exception>
struct ooops : std::exception {
const char* what() const noexcept {return "Ooops! It is a identity error\n";}
};
int main () {
try {
throw ooops();
} catch (std::exception& ex) {
std::cout << ex.what();
}
return 0;
}
示例输出应该是这样的 −
Ooops! It is a identity error

