c++
标准库
中的异常体系以std::exception为核心基类,所有标准异常均派生自它,用于构建健壮的异常处理机制。1. std::exception定义在头文件中,提供虚函数what()返回异常描述信息。2. 逻辑错误如std::logic_error及其子类std::domain_error、std::invalid_argument、std::length_error、std::out_of_range和std::future_error,表示程序可检测的逻辑问题。3. 运行时错误如std::runtime_error及其子类std::range_error、std::
overflow
_error、std::underflow_error、std::regex_error和std::system_error,表示运行期间不可预测的错误。4. 内存相关异常std::bad_alloc在new失败时抛出,直接继承自std::exception。5. 类型转换异常包括std::bad_cast(dynamic_cast失败时抛出)和std::bad_typeid(对nullptr使用typeid时可能抛出)。6. 异常处理相关异常有std::bad_exception(用于意外异常处理,已弃用)和std::nested_exception(支持嵌套异常传递)。7. 继承关系上,std::bad_alloc、std::bad_cast、std::bad_typeid、std::bad_exception和std::nested_exception均直接继承自std::exception,而非通过logic_error或runtime_error。8. 使用建议包括优先捕获具体异常类型再捕获基类、不依赖what()返回文本做逻辑判断、自定义异常应继承std::runtime_error或std::logic_error,并确保what()函数为noexcept。该体系设计清晰,覆盖常见错误场景,合理使用可显著提升程序健壮性和可维护性。
C++标准库中的异常体系以
为核心基类,所有标准异常都派生自它。了解这一类体系有助于编写更健壮的异常处理代码。以下是标准库中常见的异常类及其继承关系的详细分析。
一、std::exception 类体系概览
是所有标准异常的基类,定义在
头文件中。它提供了一个虚函数:
该函数返回描述异常原因的C风格字符串。
标准异常主要分为两大类:
逻辑错误(logic_error)
:程序逻辑可检测到的错误,通常在运行前就可发现。
运行时错误(runtime_error)
:运行过程中发生的错误,无法在编译时预测。
二、常见的标准异常类及其用途
1. 逻辑错误(继承自
)
派生自
,用于表示违反程序逻辑的错误。常见子类包括:
表示参数超出了函数定义域。例如数学函数传入非法值。
表示参数格式或类型不合法。例如
会抛出此异常。
表示试图创建一个超出容器最大长度的对象。例如
超过
。
表示访问越界,如
或
访问无效索引。
与
和
相关的错误,如重复设置值。
这些异常通常表示程序中的编程错误,应通过代码审查和输入校验避免。
2. 运行时错误(继承自
)
表示运行期间发生的、无法提前预测的错误。常见子类有:
表示计算结果超出有效范围,如数值转换溢出。
表示算术运算上溢,如浮点数过大。
表示算术运算下溢,如浮点数趋近于零但无法表示。
正则表达式构造或匹配时出错。
(C++11 起)
封装系统级错误,如线程创建失败、文件打开失败等,通常配合
使用。
这类错误通常与外部环境或资源有关,需在运行时捕获并处理。
C函数速查手册(CHM版)
C函数速查手册(CHM版)
下载
3. 内存相关异常
当
操作符无法分配足够内存时抛出。它
不继承自的子类
,而是直接继承自
。
示例:
4. 类型转换异常
用于引用或指针类型转换失败时抛出(指针返回
不抛异常,但引用失败会抛出)。
当对
指针使用
时抛出(C++17 前可能抛出,现在通常不抛,但标准仍保留此异常)。
5. 异常处理相关异常
用于意外异常处理机制(
,已弃用),现在很少使用。
(C++11 起)
支持嵌套异常,允许捕获异常后再抛出并保留原始异常信息。配合
使用。
示例:
三、标准异常类的继承关系图(文字版)
注意:std::bad_alloc、std::bad_cast 等是直接继承自 std::exception,不通过 logic_error 或 runtime_error。
四、使用建议
优先捕获具体异常类型
,再捕获基类。例如:
返回的字符串是实现定义的,内容可能简略,不要依赖其具体文本做逻辑判断。
自定义异常建议继承
或
,便于统一处理。
使用
时注意:
函数承诺不抛异常,自定义异常也应遵守。
基本上就这些。标准异常体系设计清晰,覆盖了常见错误场景,合理使用能显著提升程序的健壮性和可维护性。
std::exceptionstd::exceptionvirtual const char* what() const noexcept;std::logic_errorstd::logic_errorstd::exceptionstd::domain_errorstd::invalid_argumentstd::stoi("abc")std::length_errorstd::vector::resize()max_size()std::out_of_rangestd::vector::at()std::string::at()std::future_errorstd::futurestd::promisestd::runtime_errorstd::runtime_errorstd::range_errorstd::overflow_errorstd::underflow_errorstd::regex_errorstd::system_errorstd::error_codestd::bad_allocnewstd::exceptionstd::exceptiontry {
int* p = new int[1000000000000LL];
} catch (const std::bad_alloc& e) {
std::cout << "内存分配失败: " << e.what() << std::endl;
}std::bad_castdynamic_castnullptrtry {
Base& b = dynamic_cast (derived_obj);
} catch (const std::bad_cast& e) {
std::cout << "类型转换失败: " << e.what() << std::endl;
}std::bad_typeidnullptrtypeidstd::bad_exceptionstd::set_unexpectedstd::nested_exceptionstd::throw_with_nestedtry {
try {
throw std::runtime_error("inner");
} catch (...) {
std::throw_with_nested(std::logic_error("outer"));
}
} catch (const std::exception& e) {
// 可递归访问嵌套异常
}std::exception
├── std::logic_error
│ ├── std::domain_error
│ ├── std::invalid_argument
│ ├── std::length_error
│ ├── std::out_of_range
│ └── std::future_error
├── std::runtime_error
│ ├── std::range_error
│ ├── std::overflow_error
│ ├── std::underflow_error
│ ├── std::regex_error
│ └── std::system_error
├── std::bad_alloc
├── std::bad_cast
├── std::bad_typeid
├── std::bad_exception
└── std::nested_exceptiontry {
// ...
} catch (const std::invalid_argument& e) {
// 处理参数错误
} catch (const std::runtime_error& e) {
// 处理运行时错误
} catch (const std::exception& e) {
// 兜底处理
}what()std::runtime_errorstd::logic_errornoexceptwhat()