#include <exception>
#include <stdexcept>
#include <iostream>
#include <vector>
void demonstrateExceptions() {
std::vector<int> vec(5);
try {
// Demonstrate out_of_range
std::cout << vec.at(10) << std::endl;
} catch (const std::out_of_range& e) {
std::cerr << "Out of range error: " << e.what() << std::endl;
}
try {
// Demonstrate bad_alloc
std::vector<int> tempVec;
std::vector<int> hugeVector(tempVec.max_size());
} catch (const std::bad_alloc& e) {
std::cerr << "Bad allocation error: " << e.what() << std::endl;
}
try {
// Demonstrate invalid_argument
std::stoi("not a number");
} catch (const std::invalid_argument& e) {
std::cerr << "Invalid argument error: " << e.what() << std::endl;
}
}
int main() {
try {
demonstrateExceptions();
} catch (const std::exception& e) {
std::cerr << "Caught unhandled exception: " << e.what() << std::endl;
}
return 0;
}