在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) {}
};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() 方法,确保返回值有效基本上就这些,不复杂但容易忽略细节。
以上就是如何自定义C++异常类 继承std exception的最佳实践的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号