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 #133

The Correct what() Override

easyExceptions

EmptyCartError derives from std::exception and wants its own message. Which declaration correctly overrides what()?

📄 Code

1#include <iostream>
2#include <exception>
3 
4class EmptyCartError : public std::exception {
5public:
6 const char* what() const noexcept override {
7 return "the cart is empty";
8 }
9};
10 
11int main() {
12 try {
13 throw EmptyCartError();
14 } catch (const std::exception& e) {
15 std::cout << e.what() << "\n";
16 }
17 return 0;
18}