class
<stdexcept>

std::out_of_range

class out_of_range;
Out-of-range exception

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

It is a standard exception that can be thrown by programs. Some components of the standard library, such as vector, deque, string and bitset also throw exceptions of this type to signal arguments out of range.

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

Members

constructor
The string passed as what_arg has the same content as the value returned by member what.

The class inherits the what member function from logic_error.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// out_of_range example
#include <iostream>       // std::cerr
#include <stdexcept>      // std::out_of_range
#include <vector>         // std::vector

int main (void) {
  std::vector<int> myvector(10);
  try {
    myvector.at(20)=100;      // vector::at throws an out-of-range
  }
  catch (const std::out_of_range& oor) {
    std::cerr << "Out of Range error: " << oor.what() << '\n';
  }
  return 0;
}

Possible output:

Out of Range error: vector::_M_range_check


Exception safety

Strong guarantee: if the constructor throws an exception, there are no side effects.

See also