Wrap Legacy Error Codes in a File Error
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.
Your Output
Run your code to see the output here...