C++ New Library - bad_alloc
Description
This exception thrown on failure allocating memory and type of the exceptions thrown by the standard definitions of operator new and operator new[] when they fail to allocate the requested storage space.
Following is the declaration for std::bad_alloc.
class bad_alloc;
Parameters
none
Return Value
none
Exceptions
No-throw guarantee − this member function never throws exceptions.
Data races
none
Example
In below example for 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';
}
}
The output should be like this −
It will throw an exception error
new.htm
Advertisements