You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
27 lines
622 B
27 lines
622 B
1 year ago
|
#include <iostream>
|
||
|
#include <stdexcept>
|
||
|
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;
|
||
|
}
|