// STL6.cp #include #include /* The exception hierarchy: exception root of all exceptions | | | bad_alloc memory allocation exception | logic_error precondition violation exception root | | | | | out_of_range attempt to reference a container | | using an illegal index | | | length_error attempt to create a string with an | illegal length | invalid_argument attempt ot create a container with an illegal size such as -1 */ int main() { const int limit = 10; std::vector v; for (int j = 0; j < limit; ++j) { v.push_back(j); } try { int i = v[limit]; std::cout << i << " no exception for i = v[limit];" << std::endl; i = v.at(limit); std::cout << i << " no exception i = v.at(limit);" << std::endl; i = v.at(limit + 1); std::cout << i << " no exception i = v.at(limit + 1);" << std::endl; } catch (std::out_of_range& error) { std::cout << "An out of range execption was caught" << std::endl; } } // 0 no exception for i = v[limit]; // An out of range execption was caught