C++ 異常庫 - bad_alloc



描述

這是在分配記憶體失敗時丟擲的異常。

宣告

以下是 std::bad_alloc 的宣告。

class bad_alloc;

C++11

class bad_alloc;

引數

返回值

異常

無異常保證 - 成員不丟擲異常。

示例

以下為 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
廣告

© . All rights reserved.