bad_alloc
描述 (Description)
当分配内存和操作符new和operator new []的标准定义抛出的异常类型时,如果它们未能分配所请求的存储空间,则抛出此异常。
以下是std :: bad_alloc的声明。
class bad_alloc;
参数 (Parameters)
没有
返回值 (Return Value)
没有
异常 (Exceptions)
No-throw guarantee - 此成员函数永远不会抛出异常。
数据竞争 (Data races)
没有
例子 (Example)
在下面的示例中为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