#include <exception>
#include <string>
#include <iostream>
class CustomException : public std::exception {
private:
std::string message;
public:
explicit CustomException(const std::string& msg) : message(msg) {}
const char* what() const noexcept override {
return message.c_str();
}
};
void riskyFunction(int value) {
if (value < 0) {
throw CustomException("Negative value not allowed");
}
std::cout << "Processing value: " << value << std::endl;
}
int main() {
try {
riskyFunction(5);
riskyFunction(-1);
} catch (const CustomException& e) {
std::cerr << "Custom exception caught: " << e.what() << std::endl;
} catch (const std::exception& e) {
std::cerr << "Standard exception caught: " << e.what() << std::endl;
}
return 0;
}