CJCoding With Joseph
15free / daySign inCreate account

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.

Hints4 available
Topics:
Exceptions
📥 Auto-Input:
5\n-2\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.