#include <iostream>
class MyException : public std::exception {
public:
const char *what() const throw() {
return "Custom Exception: an error occurred!";
}
};
int divide(int a, int b) {
if (b == 0) {
throw MyException();
}
return a / b;
}
int main() {
try {
int result = divide(10, 2);
std::cout << "Result: " << result << std::endl;
result = divide(5, 0);
std::cout << "Result: " << result << std::endl;
} catch (const MyException &e) {
std::cerr << "Caught exception: " << e.what() << std::endl;
} catch (const std::exception &e) {
std::cerr << "Caught exception: " << e.what() << std::endl;
} catch (...) {
std::cerr << "Caught unknown exception" << std::endl;
}
return 0;
}