#include#include using namespace std; int main() { string str = "hello world"; char ch = str.at(6); cout << ch; return 0; }
#includeIn this example, we try to access a character at an index that is out of range. This will cause an out of range exception to be thrown. We use a try-catch block to handle this exception and print an error message. The output of this program will be "Exception occurred: basic_string::at: __n (which is 10) >= this->size() (which is 5)". Package/Library: STL (Standard Template Library)#include using namespace std; int main() { string str = "hello"; try { char ch = str.at(10); cout << ch; } catch(const exception& e) { cout << "Exception occurred: " << e.what() << endl; } return 0; }