Catch out_of_range exception : out_of_range exception « STL Basics « C++






Catch out_of_range exception

 
 


#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';
  }
}

        
  








Related examples in the same category

1.Access out-of-range element
2.Use std::out_of_range Exception for vector