What is the output of the following C++ program on a 64-bit system?
#include <iostream>
#include <stdexcept>
#include <string>
class HttpError : public std::runtime_error {
int status_;
public:
HttpError(int s, const std::string& msg) : std::runtime_error(msg), status_(s) {}
int status() const noexcept { return status_; }
bool retryable() const noexcept { return status_ >= 500; }
};
void fetch(int code) { if (code != 200) throw HttpError(code, "HTTP " + std::to_string(code)); }
int main() {
int retries = 0;
for (int code : {200, 404, 503, 500}) {
try { fetch(code); std::cout << "ok "; }
catch (const HttpError& e) {
std::cout << e.what() << (e.retryable() ? "(retry) " : " ");
retries += e.retryable();
}
}
std::cout << retries << '\n';
return 0;
}
The exception object carries the status code, letting the handler decide what to do.