CJCoding With Joseph
15free / daySign inCreate account

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.

Hints4 available
Topics:
Exceptions
📥 Auto-Input:
3\n-1\n12\n
This input is automatically fed to your program's stdin
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.