[C++] メモリ確保エラーをキャッチする方法

方法

std::bad_alloc

#include <new>
try {
  unsigned char* buffer = new unsigned char[size + 1];
} catch (std::bad_alloc) {
  // 
}

GetLastError

try {
  unsigned char* buffer = new unsigned char[size + 1];
} catch (...) {
  int err = GetLastError();
  if (ERROR_NOT_ENOUGH_MEMORY == err) {
    //
  }
}