CJCoding With Joseph
15free / daySign inCreate account

Wrap Legacy Error Codes in a File Error

Old code reports problems with numbers: the provided legacyOpen(path) returns 0 for success, -1 when the file is not found, -2 when permission is denied, and other negative numbers for anything else. Wrap it so callers get exceptions instead:

- FileError inherits from std::runtime_error; FileError(const std::string& path, int code, const std::string& reason) makes the what() text the path, : and the reason, and stores the code for int code() const - void openOrThrow(const std::string& path): calls legacyOpen(path) once and keeps the result; 0 prints opened and the path; -1 throws FileError(path, -1, "not found"); -2 throws FileError(path, -2, "permission denied"); any other code throws FileError with that code and the reason unknown error followed by the code

The tests read paths from input, one per line, and print the opened line, or error , the code, : and what(). For the first test the paths are notes.txt, missing.txt, secret.txt and corrupt.txt.

Do NOT write a main function.

Expected Output:

opened notes.txt
error -1: missing.txt: not found
error -2: secret.txt: permission denied
error -9: corrupt.txt: unknown error -9

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

Hints4 available
Topics:
Exceptions
📥 Auto-Input:
notes.txt\nmissing.txt\nsecret.txt\ncorrupt.txt\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.