C++异常封装处理 统一异常处理接口

定义统一异常类AppException并结合宏throw_APP_EXCEPTION,在模块边界捕获底层异常并转换为统一类型,最后在main函数中通过顶层try-catch捕获AppException进行集中错误处理,提升系统稳定性与可维护性。

C++异常封装处理 统一异常处理接口

c++项目中,异常处理的统一性和可维护性对系统稳定性至关重要。直接使用标准异常或裸 throw 很容易导致代码分散、错误信息不一致。通过封装异常并提供统一处理接口,可以显著提升代码的健壮性和可读性。

定义统一异常类

创建一个自定义异常类,继承std::exception,用于封装错误码、错误消息和上下文信息。

例如:

 class AppException : public std::exception { private:     int m_errorCode;     std::String m_message;     std::string m_file;     int m_line; <p>public: AppException(int code, const std::string& msg, const std::string& file, int line) : m_errorCode(code), m_message(msg), m_file(file), m_line(line) {}</p><pre class='brush:php;toolbar:false;'>const char* what() const noexcept override {     std::ostringstream oss;     oss << "[" << m_file << ":" << m_line << "] "         << "Error " << m_errorCode << ": " << m_message;     m_what = oss.str();     return m_what.c_str(); }  int errorCode() const { return m_errorCode; } std::string file() const { return m_file; } int line() const { return m_line; }

private: mutable std::string m_what; };

使用宏简化抛出异常:

立即学习C++免费学习笔记(深入)”;

 #define THROW_APP_EXCEPTION(code, msg)      throw AppException(code, msg, __FILE__, __LINE__) 

建立异常处理中间层

在关键函数入口或模块边界设置异常捕获和转换机制,将底层异常转化为统一异常类型。

例如,在服务层封装中:

 std::string UserService::getUser(int id) {     try {         return database.query("SELECT name FROM users WHERE id = " + std::to_string(id));     }     catch (const std::out_of_range& e) {         THROW_APP_EXCEPTION(1001, "Invalid user ID provided");     }     catch (const std::runtime_error& e) {         THROW_APP_EXCEPTION(2001, "Database Access failed: " + std::string(e.what()));     }     catch (...) {         THROW_APP_EXCEPTION(9999, "Unknown error occurred");     } } 

全局异常处理器

在程序主流程中设置顶层异常捕获,确保未处理异常也能被记录和响应。

main 函数示例:

 int main() {     try {         app.run();     }     catch (const AppException& e) {         std::cerr << "Application Error: " << e.what() << std::endl;         // 可集成日志系统         Logger::error(e.what());         return e.errorCode();     }     catch (const std::exception& e) {         std::cerr << "Standard Exception: " << e.what() << std::endl;         Logger::error("Standard exception: " + std::string(e.what()));         return -1;     }     catch (...) {         std::cerr << "Unknown exception caught" << std::endl;         Logger::error("Unknown exception");         return -99;     }     return 0; } 

异常日志与调试支持

在异常类中加入时间戳、调用(可选)等信息,便于问题追踪。

建议在 what() 中包含:

  • 文件名和行号(通过 __FILE__ 和 __LINE__)
  • 错误发生时间
  • 可选:线程ID,用于多线程环境

可通过工具函数生成栈跟踪(linux下使用 backtrace + backtrace_symbols)。

基本上就这些。封装异常不是为了增加复杂度,而是让错误处理更集中、信息更完整。定义好错误码体系,配合日志,能大幅降低后期维护成本。不复杂但容易忽略。

© 版权声明
THE END
喜欢就支持一下吧
点赞10 分享