std::bad_alloc
| 定义于头文件 <new>
|
||
class bad_alloc;
|
||
std::bad_alloc 是分配函数在分配存储失败时抛出的异常对象的类型。
继承关系图
成员函数
|
(constructor)
|
构造新的
bad_alloc
对象
(公开成员函数) |
|
operator=
|
替换
bad_alloc
对象
(公开成员函数) |
|
what
|
返回说明性字符串
(公开成员函数) |
std::bad_alloc:: bad_alloc
| (1) | ||
|
bad_alloc
(
)
throw
(
)
;
|
(until C++11) | |
|
bad_alloc
(
)
noexcept
;
|
(since C++11)
(constexpr since C++26) |
|
| (2) | ||
|
bad_alloc
(
const
bad_alloc
&
other
)
throw
(
)
;
|
(until C++11) | |
|
bad_alloc
(
const
bad_alloc
&
other
)
noexcept
;
|
(since C++11)
(constexpr since C++26) |
|
构造一个新的
bad_alloc
对象,其中包含一个实现定义的以空字符结尾的字节字符串,可通过
what()
访问。
std::bad_alloc
,则
std::
strcmp
(
what
(
)
, other.
what
(
)
)
==
0
。
(since C++11)
参数
| other | - | 要拷贝的另一个异常对象 |
std::bad_alloc::operator=
bad_alloc& operator=( const bad_alloc& other ) throw();
|
(C++11 前) | |
bad_alloc& operator=( const bad_alloc& other ) noexcept;
|
(C++11 起) | |
将内容赋值为 other 的内容。 如果 *this 和 other 都具有动态类型 std::bad_alloc,则在赋值后 std::strcmp(what(), other.what()) == 0。(C++11 起)
参数
| other | - | 用于赋值的另一个异常对象 |
返回值
* this
std::bad_alloc::what
virtual const char* what() const throw();
|
(C++11 前) | |
virtual const char* what() const noexcept;
|
(C++11 起) (C++26 起为 constexpr) |
|
返回解释性字符串。
返回值
指向实现定义的以空字符结尾的字符串的指针,该字符串包含解释性信息。该字符串适合转换并显示为 std::wstring 。该指针保证至少在获取它的异常对象被销毁之前,或者在异常对象上调用非 const 成员函数(例如拷贝赋值运算符)之前保持有效。
|
在常量求值期间,返回的字符串使用普通字面量编码进行编码。 |
(C++26 起) |
注解
实现允许但不要求重写
what()
。
继承自 std:: exception
成员函数
|
[virtual]
|
销毁异常对象
(
std::exception
的虚公开成员函数)
|
|
[virtual]
|
返回解释性字符串
(
std::exception
的虚公开成员函数)
|
注释
| 功能测试 宏 | 值 | 标准 | 功能 |
|---|---|---|---|
__cpp_lib_constexpr_exceptions
|
202411L
|
(C++26) | constexpr for exception types |
示例
#include <iostream> #include <new> int main() { try { while (true) { new int[100000000ul]; } } catch (const std::bad_alloc& e) { std::cout << "Allocation failed: " << e.what() << '\n'; } }
可能的输出:
Allocation failed: std::bad_alloc
另请参阅
|
分配函数
(函数) |