recover()
GO-W1030According to the go documentation of recover
,
executing a call to recover
inside a deferred function (but not any function called
by it) stops the panicking sequence by restoring normal execution and retrieves the
error value passed to the call of panic
. If recover
is called outside the deferred
function, it will not stop a panicking sequence.
Calling recover
immediately after a go
or a defer
statement will have no
effect.
package main
func foo() {
defer recover()
go recover()
}
package main
func foo() {
defer func() {
if r := recover(); r != nil {
// panic recovered, do something
}
}()
}