CJCoding With Joseph
15free / daySign inCreate account

Unknown Item Error with a Built Message

Create an exception type UnknownItemError that inherits publicly from std::runtime_error. Its constructor takes the item name as a const std::string& and passes the text unknown item: followed by the name up to the base-class constructor, so what() returns that full message without you writing a what() of your own. Then finish priceFor(const std::string& item): the prices for pen and notebook are already there; for any other item throw an UnknownItemError built from the item name.

The tests look up a few items, printing the name, : and the price, or Error: and the message; one test catches UnknownItemError, the other catches it as a std::runtime_error, which works because of the inheritance.

Do NOT write a main function.

Expected Output:

pen: 1.5
Error: unknown item: stapler
notebook: 3.25

= newline. The output ends with a newline — print one after the last line.

Hints4 available
Topics:
Exceptions
1
Tab to indent (Esc then Tab to move on) · Ctrl+Enter to run · Shift+Alt+F to format · Ctrl+Space to expand shortcuts (cout, fori)

Your Output

Run your code to see the output here...

Test Cases

Run your code to see test case results.