example2_out_of_range.cpp

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
#include <iostream>
#include <stdexcept>
#include <vector>

int main() {
    std::vector<int> vec = {1, 2, 3};
    try {
        std::cout << vec.at(5) << std::endl;
    } catch (const std::out_of_range& e) {
        std::cerr << "Out of range error: " << e.what() << std::endl;
    }
    return 0;
}
Back to stdexcept