Bibliothèque d'exceptions C ++ - bad_alloc

La description

Il s'agit d'une exception levée en cas d'échec d'allocation de mémoire.

Déclaration

Voici la déclaration pour std :: bad_alloc.

class bad_alloc;

C ++ 11

class bad_alloc;

Paramètres

aucun

Valeur de retour

aucun

Des exceptions

No-throw guarantee - aucun membre ne lance d'exceptions.

Exemple

Dans l'exemple ci-dessous pour 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;
}