← Back to Question List
C++ Quiz #159
A what() Built From a Code and a Message
HttpError stores a code and builds its full text in the constructor. What is printed?
📄 Code
1#include <iostream>2#include <exception>3#include <string>45class HttpError : public std::exception {6 int m_code;7 std::string m_full;8public:9 HttpError(int code, const std::string& reason)10 : m_code(code), m_full("E" + std::to_string(code) + ": " + reason) {}11 int code() const { return m_code; }12 const char* what() const noexcept override { return m_full.c_str(); }13};1415int main() {16 try {17 throw HttpError(404, "page missing");18 } catch (const HttpError& e) {19 std::cout << e.what() << "\n";20 std::cout << "code " << e.code() << "\n";21 }22 return 0;23}