
class CObject {
public:
CObject() {
}
virtual void run() {
cout << "父类运行" << endl;
}
void start() {
if (m_init == 0) {
throw exception("object don't init");
}
}
private:
int m_init = 0;
};
class MyException :public exception {
private:
string m_error_msg = "";
public:
MyException(string msg) :m_error_msg(msg) {
}
const char* what() const noexcept override {
return (m_error_msg).c_str();
}
};
CObject obj;
try {
obj.start();
}
catch (std::exception e) {
cout << e.what() << endl;
}
try {
test_fun();
}
catch (const MyException &e) {
cout << e.what() << endl;
}
try {
throw MyException("其他错误");
}
catch (const MyException& e) {
cout << e.what() << endl;
}