Skip to content

Instantly share code, notes, and snippets.

@illescasDaniel
Created October 24, 2016 11:46
Show Gist options
  • Select an option

  • Save illescasDaniel/da7b288f50b9ca1f3973ab76ac7f7f4b to your computer and use it in GitHub Desktop.

Select an option

Save illescasDaniel/da7b288f50b9ca1f3973ab76ac7f7f4b to your computer and use it in GitHub Desktop.
Try catch with vector [C++]
#include <iostream>
#include <vector>
#include <stdexcept>
#define varName(_variable_) #_variable_
using namespace std;
int main(int argc, char *argv[]) {
vector<int> numbers = {1,2,3};
try {
cout << numbers.at(5) << endl;
} catch (const out_of_range& exception) {
cerr << "Error, index out of range. Variable: " << varName(numbers) << endl;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment