#include #include using namespace std; class MyException : public std::exception { public: explicit MyException(const std::string& msg) : message(msg) {} virtual const char* what() const noexcept override { return message.c_str(); } private: std::string message; }; int main() { try { std::string weapon = "手枪"; // 抛出一个异常 // throw "C++ Exception" + an; throw MyException(weapon); } catch (const char* e) { // 捕获异常并处理 cout << "Caught an exception: " << e << endl; } return 0; }