C++ New 库 - bad_alloc
描述
此异常是在分配内存失败时抛出的异常,以及 operator new 和 operator new[] 的标准定义在分配请求的存储空间失败时抛出的异常类型。
以下是 std::bad_alloc 的声明。
class bad_alloc;
参数
none
返回值
none
异常
No-throw guarantee − 这个成员函数从不抛出异常。
数据竞争
none
示例
在下面的 std::bad_alloc 示例中。
#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';
}
}
输出应该是这样的 −
It will throw an exception error

