C++ Exception Library - bad_alloc



Description

This is an exception thrown on failure allocating memory.

Declaration

Following is the declaration for std::bad_alloc.

class bad_alloc;

C++11

class bad_alloc;

Parameters

none

Return Value

none

Exceptions

No-throw guarantee − no members throw exceptions.

Example

In below example for std::bad_alloc.

#include <iostream>
#include <new>

int main () {
   try {
      int* myarray= new int[500000];
   } catch (std::bad_alloc& ba) {
      std::cerr << "bad_alloc caught: " << ba.what() << '\n';
   }
   return 0;
}
exception.htm
Advertisements