CJCoding With Joseph
15free / daySign inCreate account
← Back to Question List

Select topics to narrow your question pool, then enable Random to jump to a random question matching your filters.

Topics:
C++ Quiz #159

A what() Built From a Code and a Message

hardExceptions

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>
4 
5class 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};
14 
15int 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}