#include <iostream>
#include <string>
class NetworkError : public std::exception {
private:
std::string message;
public:
NetworkError(const std::string& msg) : message(msg) {}
const char* what() const noexcept override {
return message.c_str();
}
};
void connectToServer(const std::string& server) {
if (server.empty()) {
throw NetworkError("Empty server address");
}
if (server == "localhost") {
throw NetworkError("Cannot connect to localhost");
}
std::cout << "Connected to " << server << std::endl;
}
int main() {
try {
connectToServer("example.com");
connectToServer("localhost");
} catch (const NetworkError& e) {
std::cerr << "Network error: " << e.what() << std::endl;
}
return 0;
}