Catch out_of_range exception : out_of_range exception « STL Introduction « C++ Tutorial






#include <iostream>
#include <vector>
#include <exception>
#include <stdexcept> // out_of_range exception
using namespace std;

int main( ) {
  char carr[] = {'a', 'b', 'c', 'd', 'e'};

  vector<char> v;
  v.push_back('a');
  v.push_back('b');
  v.push_back('c');
  v.push_back('d');
  v.push_back('e');

  try {
    cout << v.at(10000) << '\n';
  } catch(std::out_of_range& e) {
    cerr << e.what( ) << '\n';
  }
}








14.7.out_of_range exception
14.7.1.Access out-of-range element
14.7.2.Use std::out_of_range Exception for vector
14.7.3.Catch out_of_range exception