C++ Exception Library - bad_array_new_length



Description

It is an exception on bad array length and This type of array thrown in any of the following cases −

  • If the array size is less than zero.
  • If the array size is greater than an implementation-defined limit.
  • If the number of elements in the initializer list exceeds the number of elements to initialize.

Declaration

Following is the declaration for std::bad_array_new_length.

class bad_array_new_length;

C++11

class bad_array_new_length;

Parameters

none

Return Value

none

Exceptions

No-throw guarantee − no members throw exceptions.

Members

  • constructor − what_arg has the same content as the value returned by member what.

  • what − It is used to get string identifying exception.

Example

In below example for std::bad_array_new_length.

#include <iostream>
#include <new>
#include <climits>
 
int main() {
   int negative = -1;
   int small = 1;
   int large = INT_MAX;
   try {
      new int[negative];           
      new int[small]{1,2,3,4};       
      new int[large][50000000];     
   } catch(const std::bad_array_new_length &e) {
      std::cout << e.what() << '\n';
   }
}

The output should be like this −

std::bad_array_new_length
exception.htm
Advertisements