使用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 <iostream>
#include <stdexcept>
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 <iostream>
#include <stdexcept>
#include <string>
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_
在C++11之前,可以使用异常规范来声明函数可能抛出的异常类型。例如:
void myFunction() throw (std::runtime_error, std::bad_alloc);
这表明
myFunction
std::runtime_error
std::bad_alloc
noexcept
使用
noexcept
void myFunction() noexcept;
这表明
myFunction
myFunction
std::terminate
因此,不建议使用C++11之前的异常规范。应该使用
noexcept
异常安全是指在异常抛出时,程序的状态仍然保持一致。这通常需要仔细设计代码,以确保资源得到正确释放,数据结构保持有效。
以下是一些保证异常安全的常用技巧:
例如,使用RAII技术来管理互斥锁:
#include <iostream>
#include <mutex>
#include <stdexcept>
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
LockGuard
LockGuard
总而言之,
std::exception
以上就是C++如何使用标准异常类std::exception的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号