class

std::length_error

<stdexcept>
class length_error;
Length error exception
length_error

This class defines the type of objects thrown as exceptions to report a length error.

This class is designed so that any program, not just the elements of the standard library, can throw it as an exception. Some members of vector and string throw this error when attempted to resize to an excessive length.

It is defined as:
1
2
3
4
class length_error : public logic_error {
public:
  explicit length_error (const string& what_arg);
};


Members

constructor
The constructor takes a standard string object as parameter. This value is stored in the object, and its value is used to generate the C-string returned by its inherited member what.

The class inherits the what member function from exception, along with its copy constructor and assignment operator member functions.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// length_error example
#include <iostream>
#include <stdexcept>
#include <vector>
using namespace std;

int main (void) {
  try {
    // vector throws a length_error if resized above max_size
    vector<int> myvector;
    myvector.resize(myvector.max_size()+1);
  }
  catch (length_error& le) {
	  cerr << "Length error: " << le.what() << endl;
  }
  return 0;
}


Possible output:

Length error: vector::_M_fill_insert

See also