One Catch for a Whole Family of Shop Errors
One Catch for a Whole Family of Shop Errors
Because what() is virtual, a single catch (const std::exception&) can report every member of an exception family with the right text. Build the family and the function:
- ShopError inherits from std::runtime_error; its explicit constructor takes a const std::string& message and passes it on
- PaymentError, StockError and ShippingError inherit from ShopError; each constructor takes a const std::string& detail and passes payment: , stock: or shipping: joined with the detail up to ShopError
- void placeOrder(int step): step 1 throws PaymentError("card declined"), step 2 throws StockError("item sold out"), step 3 throws ShippingError("no courier available"), and any other step prints order placed
The tests read steps from input, one per line, and use only catch (const std::exception& e), printing order failed: and what() or the placed line. For the first test the input is 0, 1, 2, 3.
Do NOT write a main function.
Expected Output:
order placed order failed: payment: card declined order failed: stock: item sold out order failed: shipping: no courier available
= newline. The output ends with a newline — print one after the last line.
Your Output
Run your code to see the output here...