在c++++中,自定义异常类最常见的做法是继承std::exception或其派生类。1. 优先选择合适的基类如std::logic_error或std::runtime_error以增强语义性;2. 实现what()方法时确保返回值有效且生命周期足够长;3. 可添加错误码、文件名等额外信息便于调试处理;4. 异常类应保持轻量避免复杂资源管理逻辑。例如通过继承std::runtime_error并传递错误信息至基类构造函数实现基本异常结构,并可扩展成员函数获取具体上下文数据。

在C++中,自定义异常类最常见的做法是继承标准库中的 std::exception 或其派生类。这样做不仅可以让异常体系更清晰,还能与标准异常兼容,方便统一处理。

选择合适的基类
std::exception 是所有标准异常的基类,但它本身是一个抽象类(有虚析构函数和一个虚函数 what())。不过直接继承它有时并不方便,因为标准库已经提供了一些更有意义的派生类:

-
std::logic_error:用于程序逻辑错误,比如非法参数、断言失败等。 -
std::runtime_error:用于运行时错误,比如文件打开失败、网络问题等。
根据你的异常类型选择合适的基类,可以让你的代码更具语义性。例如:
立即学习“C++免费学习笔记(深入)”;
class my_exception : public std::runtime_error {
public:
explicit my_exception(const std::string& msg) : std::runtime_error(msg) {}
};实现 what() 方法
std::exception 要求你实现 virtual const char* what() const noexcept 方法。如果你继承的是 std::logic_error 或 std::runtime_error,它们已经帮你实现了这个方法,并且会返回构造时传入的字符串。

但如果你直接继承 std::exception,你需要自己实现这个方法。常见做法是保存一个 std::string 成员变量并返回它的 c_str():
class my_exception : public std::exception {
std::string msg_;
public:
explicit my_exception(const std::string& msg) : msg_(msg) {}
const char* what() const noexcept override { return msg_.c_str(); }
};注意几点:
- 确保
what()是noexcept - 返回值生命周期要足够长,不能返回临时字符串的指针
- 可以考虑加上虚拟继承,避免多继承时出问题
添加额外信息
除了标准接口外,你可以为异常类添加更多有用的成员函数或数据。比如:
- 错误码
- 源文件/行号
- 出错的函数名或模块名
这样调用者可以根据这些信息做更细粒度的判断或日志记录。例如:
class file_not_found_exception : public std::runtime_error {
std::string filename_;
int error_code_;
public:
file_not_found_exception(const std::string& filename, int code)
: std::runtime_error("File not found: " + filename), filename_(filename), error_code_(code) {}
const std::string& filename() const { return filename_; }
int error_code() const { return error_code_; }
};使用时就可以:
try {
// ...
} catch (const file_not_found_exception& e) {
std::cerr << "Error " << e.error_code() << " opening file " << e.filename() << std::endl;
}总结一下
继承 std::exception 的最佳实践包括:
- 根据异常类型选择合适的基类,如
std::logic_error或std::runtime_error - 正确实现
what()方法,确保返回值有效 - 在异常对象中携带更多上下文信息,便于调试和处理
- 异常类应尽量轻量,不要包含复杂资源管理逻辑
基本上就这些,不复杂但容易忽略细节。










