Guard Destructor Runs Before the Catch
Guard Destructor Runs Before the Catch
When an exception leaves a function, every local object in that function is destroyed on the way out, BEFORE the catch block runs. Show it:
- Guard: explicit Guard(const std::string& name) stores the name and prints open followed by the name; ~Guard() prints close followed by the name (each on its own line)
- void process(int value): creates a Guard named file as its first statement; a value below 0 then throws std::runtime_error("negative value"), otherwise it prints processed and the value
The tests read values from input, one per line; each is passed to process inside a try block that prints done after a normal return, or caught: and what() after an exception. For the first test the input is 5 then -2. The closing line of the guard appears before done and before caught, in both cases.
Do NOT write a main function.
Expected Output:
open file processed 5 close file done open file close file caught: negative value
= newline. The output ends with a newline — print one after the last line.
Your Output
Run your code to see the output here...