Reference
C Library
IOstream Library
Strings library
STL Containers
STL Algorithms
Miscellaneous
Miscellaneous
complex
exception
functional
iterator
limits
locale
memory
new
numeric
stdexcept
typeinfo
utility
valarray
stdexcept
domain_error
invalid_argument
length_error
logic_error
out_of_range
overflow_error
range_error
runtime_error
underflow_error


out_of_range

class
<stdexcept>
class out_of_range;

Out-of-range exception

out_of_range

This class defines the type of objects thrown as exceptions to report an out-of-range error.

This class is designed so that any program, not just the elements of the standard library, can throw it as an exception. Several members of vector, deque, string and bitset throw this error on an out-of-range error.

It is defined as:
1
2
3
4
class out_of_range : public logic_error {
public:
  explicit out_of_range (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 asignment operator member functions.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// out_of_range example
#include <iostream>
#include <stdexcept>
#include <vector>
using namespace std;
int main (void) {
  vector<int> myvector(10);
  try {
    myvector.at(20)=100;      // vector::at throws an out-of-range
  }
  catch (out_of_range& oor) {
    cerr << "Out of Range error: " << oor.what() << endl;
  }
  return 0;
}


Possible output:

Out of Range error: vector::_M_range_check

See also