Created
October 24, 2016 11:46
-
-
Save illescasDaniel/da7b288f50b9ca1f3973ab76ac7f7f4b to your computer and use it in GitHub Desktop.
Try catch with vector [C++]
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #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