// example from https://en.cppreference.com/w/cpp/string/basic_string/at // #include #include #include // added this and removed std:: throughout, RP: // using namespace std; int main() { string s("message"); // for capacity s = "abc"; s.at(2) = 'x'; // ok cout << s << '\n'; cout << "string size = " << s.size() << '\n'; cout << "string capacity = " << s.capacity() << '\n'; try { // throw, even if capacity allowed to access element s.at(3) = 'x'; } // catch (out_of_range const& exc) { // simplified catch, RP: // catch (const exception& exc) { cout << exc.what() << '\n'; } }