Recover
This lesson is a guide for programmers to recover their code from panic or any error condition.
We'll cover the following...
We'll cover the following...
What is recover()
function?
As the name indicates, this built-in function can be used to recover from a panic or an error-condition: it allows a program to regain control of a panicking Go routine, stopping the terminating sequence and resuming normal execution.
The recover
is only useful when called inside a deferred function: it then retrieves the error value passed through the call to panic
. When used in normal execution, a call to recover
will return nil and have no other effect. The panic
causes the stack to unwind until a deferred recover()
is found or the program terminates.
Explanation
The protect
function in the example below invokes the function argument g
and protects ...