← Back to Exceptions
Question 154
Rethrowing Exceptions
Rethrowing Exceptions
Write two functions that demonstrate logging and rethrowing an exception:
1. loadFile(const string& filename): if filename is empty, throw a std::runtime_error with "Empty filename". Otherwise print "Loading: " followed by filename.
2. openDocument(const string& filename): calls loadFile inside a try block. If a runtime_error is caught, print "[LOG] " followed by e.what(), then rethrow the same exception using throw; (bare throw with no argument).
The caller of openDocument handles the final exception.
Do NOT write a main function.
Expected Output:
Loading: report.pdf= newline. The output ends with a newline — print one after the last line.
Hints3 available
Sign in to use hintsTopics:
FunctionsExceptions
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.