<new>

std::bad_array_new_length

class bad_array_new_length;
陣列長度錯誤的異常

在以下任何情況下,由陣列 new 表示式丟擲的異常的型別
  • 如果陣列大小為負數。
  • 如果陣列大小大於實現定義的限制。
  • 如果初始化列表中的元素數量超過要初始化的元素數量。

此類派生自 bad_allocexception 派生自 exception)。有關標準異常的成員定義,請參閱 exception 類。

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

示例

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

int main() {
  try {
    int* p = new int[-1];
  } catch (std::bad_array_new_length& e) {
    std::cerr << "bad_array_new_length caught: " << e.what() << '\n';
  } catch (std::exception& e) {   // older compilers may throw other exceptions:
    std::cerr << "some other standard exception caught: " << e.what() << '\n';
  }
}

可能的輸出

bad_array_new_length caught: bad array new length


異常安全

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

另見