<new>

std::bad_alloc

class bad_alloc;
在分配記憶體失敗時丟擲的異常

當標準定義的 operator newoperator new[] 無法分配請求的儲存空間時丟擲的異常的型別。

此類繼承自 exception。有關標準異常的成員定義,請參閱 exception 類。

其成員 what 返回一個標識異常的空終止字元序列

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// bad_alloc example
#include <iostream>     // std::cout
#include <new>          // std::bad_alloc

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

可能的輸出

bad_alloc caught: bad allocation


異常安全

無異常保證:無成員丟擲異常。

另見