C++中的异常非常简单。

就是维护一个字符串msg,然后用what()返回这个字符串msg

1
2
3
4
5
6
7
8
9
class exception {
private:
std::string msg;
public:
explicit exception(const char* msg) : msg(msg) {}
const char* what() const {
return msg.c_str();
}
}

就是这样。自己构造一个类继承exception也是这样写。

1
2
3
4
5
6
7
8
9
class MyException : public exception {
private:
std::string msg;
public:
explicit MyException(const char* msg) : msg(msg) {}
const char* what() const override {
return msg.c_str();
}
};