C++ 标准库中的异常类
1. std::exception
基类: std::exception
是所有标准库异常类的基类。它提供了一个虚函数 what()
,返回一个描述异常的 C 风格字符串。使用: 所有标准异常类都继承自 std::exception
,因此可以捕获任何标准异常。
# include <exception>
# include <iostream>
int main ( ) {
try {
throw std:: exception ( ) ;
} catch ( const std:: exception& e) {
std:: cerr << "Caught exception: " << e. what ( ) << std:: endl;
}
return 0 ;
}
2. std::logic_error
基类: std::logic_error
是一个继承自 std::exception
的类,表示程序逻辑上的错误,通常是可以通过修改代码来避免的错误。构造函数: 接受一个 const char*
或 const std::string&
作为错误消息。
# include <stdexcept>
# include <iostream>
int main ( ) {
try {
throw std:: logic_error ( "Logic error occurred" ) ;
} catch ( const std:: logic_error& e) {
std:: cerr << "Caught logic error: " << e. what ( ) << std:: endl;
}
return 0 ;
}
派生类
std::domain_error
描述: 表示函数接收到超出其定义域的参数。例子: 计算负数的平方根。 # include <cmath>
# include <stdexcept>
# include <iostream>
double calculateSquareRoot ( double value) {
if ( value < 0 ) {
throw std:: domain_error ( "Cannot calculate square root of a negative number." ) ;
}
return std:: sqrt ( value) ;
}
int main ( ) {
try {
std:: cout << calculateSquareRoot ( - 4 ) << std:: endl;
} catch ( const std:: domain_error& e) {
std:: cerr << "Domain error: " << e. what ( ) << std:: endl;
}
return 0 ;
}
std::invalid_argument
描述: 表示传递了无效参数给函数。例子: 一个函数期望数字但传递了字符串。 # include <stdexcept>
# include <iostream>
void processValue ( int value) {
if ( value < 0 ) {
throw std:: invalid_argument ( "Negative value provided." ) ;
}
std:: cout << "Processing value: " << value << std:: endl;
}
int main ( ) {
try {
processValue ( - 1 ) ;
} catch ( const std:: invalid_argument& e) {
std:: cerr << "Invalid argument: " << e. what ( ) << std:: endl;
}
return 0 ;
}
std::length_error
描述: 表示长度错误,通常是容器超出了其最大大小。例子: 尝试创建一个超出最大允许大小的容器。 # include <stdexcept>
# include <iostream>
# include <string>
void createLargeString ( size_t size) {
if ( size > std:: string:: npos) {
throw std:: length_error ( "String size exceeds maximum allowed length." ) ;
}
std:: string str ( size, 'a' ) ;
std:: cout << "Created string of size: " << size << std:: endl;
}
int main ( ) {
try {
createLargeString ( std:: string:: npos + 1 ) ;
} catch ( const std:: length_error& e) {
std:: cerr << "Length error: " << e. what ( ) << std:: endl;
}
return 0 ;
}
std::out_of_range
描述: 表示访问超出了容器的有效范围。例子: 尝试访问数组或容器中不存在的元素。 # include <stdexcept>
# include <iostream>
# include <vector>
void accessElement ( const std:: vector< int > & vec, size_t index) {
if ( index >= vec. size ( ) ) {
throw std:: out_of_range ( "Index is out of range." ) ;
}
std:: cout << "Element at index " << index << ": " << vec[ index] << std:: endl;
}
int main ( ) {
std:: vector< int > vec = { 1 , 2 , 3 } ;
try {
accessElement ( vec, 5 ) ;
} catch ( const std:: out_of_range& e) {
std:: cerr << "Out of range error: " << e. what ( ) << std:: endl;
}
return 0 ;
}
3. std::runtime_error
基类: std::runtime_error
也是一个继承自 std::exception
的类,表示程序在运行时发生的错误,通常是无法通过修改代码避免的错误。构造函数: 接受一个 const char*
或 const std::string&
作为错误消息。
# include <stdexcept>
# include <iostream>
int main ( ) {
try {
throw std:: runtime_error ( "Runtime error occurred" ) ;
} catch ( const std:: runtime_error& e) {
std:: cerr << "Caught runtime error: " << e. what ( ) << std:: endl;
}
return 0 ;
}
派生类
std::overflow_error
描述: 表示算术运算导致的溢出错误。例子: 一个整数超出了其最大值。 # include <stdexcept>
# include <iostream>
# include <limits>
void addIntegers ( int a, int b) {
if ( a > std:: numeric_limits < int > :: max ( ) - b) {
throw std:: overflow_error ( "Integer addition overflow." ) ;
}
std:: cout << "Result: " << ( a + b) << std:: endl;
}
int main ( ) {
try {
addIntegers ( std:: numeric_limits < int > :: max ( ) , 1 ) ;
} catch ( const std:: overflow_error& e) {
std:: cerr << "Overflow error: " << e. what ( ) << std:: endl;
}
return 0 ;
}
std::underflow_error
描述: 表示算术运算导致的下溢错误。例子: 浮点数运算结果小于可表示的最小正数。 # include <stdexcept>
# include <iostream>
# include <limits>
void subtractFloats ( float a, float b) {
if ( a - b < std:: numeric_limits < float > :: min ( ) ) {
throw std:: underflow_error ( "Float subtraction underflow." ) ;
}
std:: cout << "Result: " << ( a - b) << std:: endl;
}
int main ( ) {
try {
subtractFloats ( std:: numeric_limits < float > :: min ( ) , 1.0f ) ;
} catch ( const std:: underflow_error& e) {
std:: cerr << "Underflow error: " << e. what ( ) << std:: endl;
}
return 0 ;
}
总结