异常处理
1 目标
本文的目的是学习异常处理。
2 示例代码
在 C++ 中,异常处理通过 throw 语句抛出异常,并使用 try-catch 块捕获异常。这种机制可以在运行时处理错误情况,确保程序的稳定性。
#include <iostream>
#include <stdexcept>
#include <string>
// 简单的异常类定义
class MyException : public std::exception {
private:
std::string message;
public:
MyException(const std::string& msg) : message(msg) {
}
const char* what