← Back to Question List
C++ Quiz #133
The Correct what() Override
EmptyCartError derives from std::exception and wants its own message. Which declaration correctly overrides what()?
📄 Code
1#include <iostream>2#include <exception>34class EmptyCartError : public std::exception {5public:6 const char* what() const noexcept override {7 return "the cart is empty";8 }9};1011int main() {12 try {13 throw EmptyCartError();14 } catch (const std::exception& e) {15 std::cout << e.what() << "\n";16 }17 return 0;18}