Device Error with a Code and a Message
Device Error with a Code and a Message
An exception can carry more than a sentence. Build DeviceError, which inherits from std::runtime_error and also stores an integer code:
- DeviceError(int code, const std::string& message): the what() text is E, then the code, then : , then the message (for example E100: negative id); the code is kept in a private member
- int code() const: returns the code
Then write void openDevice(int id): an id below 0 throws DeviceError(100, "negative id"), an id above 9 throws DeviceError(200, "no such device"), and otherwise it prints device , the id and open on one line.
The tests read ids from input, one per line, call openDevice for each, and on an error print failed with code , the code, : and what(). For the first test the input is 3, -1, 12.
Do NOT write a main function.
Expected Output:
device 3 open failed with code 100: E100: negative id failed with code 200: E200: no such device
= newline. The output ends with a newline — print one after the last line.
Your Output
Run your code to see the output here...