使用std::exception可构建健壮代码,其继承体系提供标准错误处理机制;应合理使用标准异常类如std::invalid_argument,并在需传递额外信息时自定义异常类;避免使用已废弃的异常规范,改用noexcept;通过RAII等技术保证异常安全,防止资源泄漏。

C++中使用
std::exception
,本质上是为了构建更健壮、更易于维护的代码。它提供了一种标准的、结构化的方式来处理程序运行期间可能出现的各种错误。不必每次都手动构建错误处理机制,而是可以依赖于这个预定义的类及其派生类。
使用
std::exception
,核心在于理解其继承体系,并在适当的时候抛出和捕获异常。
解决方案:
C++标准库提供了一系列从
std::exception
派生的异常类,用于表示不同类型的错误。例如:
立即学习“C++免费学习笔记(深入)”;
std::bad_alloc
: 当
new
操作符无法分配内存时抛出。
std::bad_cast
: 当使用
dynamic_cast
进行类型转换失败时抛出。
std::invalid_argument
: 当函数接收到无效参数时抛出。
std::out_of_range
: 当试图访问超出范围的容器元素时抛出。
std::runtime_error
: 用于报告运行时发生的错误。
std::logic_error
: 用于报告程序逻辑上的错误。
以下是一个使用
std::exception
的简单例子:
#include #include int divide(int a, int b) { if (b == 0) { throw std::invalid_argument("Division by zero is not allowed."); } return a / b;}int main() { try { int result = divide(10, 0); std::cout << "Result: " << result << std::endl; } catch (const std::invalid_argument& e) { std::cerr << "Error: " << e.what() << std::endl; return 1; } catch (const std::exception& e) { std::cerr << "An unexpected error occurred: " << e.what() << std::endl; return 1; } catch (...) { std::cerr << "Unknown exception caught!" << std::endl; return 1; } return 0;}
在这个例子中,
divide
函数在除数为零时抛出一个
std::invalid_argument
异常。
main
函数中的
try-catch
块捕获这个异常,并打印错误信息。注意,最后的
catch(...)
可以捕获所有未被前面
catch
块处理的异常,但通常不建议过度使用,因为它会隐藏具体的错误类型。
何时应该自定义异常类?
并非所有错误都需要使用标准异常类。在某些情况下,自定义异常类可能更合适。例如,当需要传递额外的错误信息,或者需要区分特定于应用程序的错误类型时,自定义异常类就显得很有必要。
自定义异常类通常从
std::exception
或其派生类继承,并添加自己的成员变量和方法来存储和访问错误信息。
#include #include #include class MyCustomException : public std::runtime_error {public: MyCustomException(const std::string& message, int errorCode) : std::runtime_error(message), errorCode_(errorCode) {} int getErrorCode() const { return errorCode_; }private: int errorCode_;};int processData(int data) { if (data < 0) { throw MyCustomException("Data is invalid.", 1001); } return data * 2;}int main() { try { int result = processData(-5); std::cout << "Result: " << result << std::endl; } catch (const MyCustomException& e) { std::cerr << "Custom Exception caught: " << e.what() << ", Error Code: " << e.getErrorCode() << std::endl; return 1; } catch (const std::exception& e) { std::cerr << "Standard Exception caught: " << e.what() << std::endl; return 1; } return 0;}
在这个例子中,
MyCustomException
继承自
std::runtime_error
,并添加了一个
errorCode_
成员变量来存储自定义的错误代码。
异常规范(Exception Specifications)是否应该使用?
在C++11之前,可以使用异常规范来声明函数可能抛出的异常类型。例如:
void myFunction() throw (std::runtime_error, std::bad_alloc);
这表明
myFunction
可能会抛出
std::runtime_error
或
std::bad_alloc
异常。然而,异常规范在C++11中被废弃,并在C++17中被移除。现在,只剩下
noexcept
规范,用于声明函数不会抛出任何异常。
使用
noexcept
可以帮助编译器进行优化,并提供更强的异常安全保证。例如:
void myFunction() noexcept;
这表明
myFunction
不会抛出任何异常。如果
myFunction
内部抛出了异常,程序会立即终止(调用
std::terminate
)。
因此,不建议使用C++11之前的异常规范。应该使用
noexcept
来声明不抛出异常的函数。
如何保证异常安全?
异常安全是指在异常抛出时,程序的状态仍然保持一致。这通常需要仔细设计代码,以确保资源得到正确释放,数据结构保持有效。
以下是一些保证异常安全的常用技巧:
RAII (Resource Acquisition Is Initialization): 使用RAII技术来管理资源,例如使用智能指针来自动释放内存。Copy-and-Swap: 在修改对象状态之前,先创建一个副本,然后在副本上进行修改。如果修改过程中发生异常,原始对象的状态不会受到影响。修改完成后,将副本与原始对象进行交换。Strong Exception Safety: 保证操作要么完全成功,要么完全不产生副作用。如果操作失败,程序的状态应该恢复到操作之前的状态。Basic Exception Safety: 保证操作不会导致资源泄漏,并且对象的状态仍然有效。No-Throw Guarantee: 保证操作不会抛出任何异常。
例如,使用RAII技术来管理互斥锁:
#include #include #include class LockGuard {public: LockGuard(std::mutex& mutex) : mutex_(mutex) { mutex_.lock(); } ~LockGuard() { mutex_.unlock(); }private: std::mutex& mutex_;};void processData(int data, std::mutex& mutex) { LockGuard lock(mutex); // Acquire lock if (data < 0) { throw std::invalid_argument("Data is invalid."); } // Process data std::cout << "Processing data: " << data << std::endl;}int main() { std::mutex mutex; try { processData(-5, mutex); } catch (const std::exception& e) { std::cerr << "Exception caught: " << e.what() << std::endl; return 1; } return 0;}
在这个例子中,
LockGuard
类使用RAII技术来管理互斥锁。当
LockGuard
对象被创建时,互斥锁被锁定。当
LockGuard
对象被销毁时(无论是因为正常退出还是因为异常抛出),互斥锁都会被自动解锁。这确保了互斥锁总是被正确释放,即使在异常情况下也是如此。
总而言之,
std::exception
是C++异常处理的基础。理解其继承体系,合理使用标准异常类,并在必要时自定义异常类,可以帮助编写更健壮、更易于维护的代码。同时,注意保证异常安全,避免资源泄漏和数据损坏。
以上就是C++如何使用标准异常类std::exception的详细内容,更多请关注创想鸟其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1475956.html
微信扫一扫
支付宝扫一扫