CJCoding With Joseph
15free / daySign inCreate account

Catch and Report Any Task

Write void catchAndReport(void (*task)(), const std::string& label), a safety net around a function it knows nothing about. It calls task() inside a try block and prints exactly one line:

- when the task finishes normally: the label, then : ok - when the task throws anything derived from std::exception: the label, then : failed (, what() and ) - when the task throws anything else: the label, then : failed (unknown error)

No exception may escape catchAndReport. The tests define a few tasks of their own (one prints and returns, one throws a std::runtime_error, one throws a plain int, one throws a std::logic_error), call catchAndReport for each with a label, and print all tasks reported at the end.

Do NOT write a main function.

Expected Output:

copying files
backup: ok
upload: failed (no connection)
cleanup: failed (unknown error)
verify: failed (checksum mismatch)
all tasks reported

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

Hints4 available
Topics:
Exceptions
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.