After a `recover` successfully handles a panic inside a function `doWork()`, where does execution control return to?

Go & Rust interview question for Advanced practice.

Answer

The doWork() function returns, and execution continues at the statement after the call to doWork() in main.

Explanation

After recover is called, the panic is stopped. The deferred function finishes executing, and then the panicking function (doWork) returns to its caller. Execution then resumes normally in the calling function (main) at the point immediately following the call to the panicking function. Therefore, 'Returned from doWork.' will be printed.

Related Questions