c++++标准库中的异常体系以std::exception为核心基类,所有标准异常均派生自它,用于构建健壮的异常处理机制。1. std::exception定义在

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










