Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why recover() does not work in a nested deferred function?

I am testing panic/recover in Golang. This simple program works as expected:

package main

import "fmt"

func printRecover() {
    r := recover()
    fmt.Println("Recovered:", r)
}

func main() {
    defer printRecover()

    panic("OMG!")
}

Output:

Recovered: OMG!

However, if I wrap the function printRecover() in a bigger deferred function:

package main

import "fmt"

func printRecover() {
    r := recover()
    fmt.Println("Recovered:", r)
}

func main() {
    defer func() {
        printRecover()
    }()

    panic("OMG!")
}

It does not recover and let the panic go through:

Recovered: <nil>
panic: OMG!

goroutine 1 [running]:
main.main()
    /tmp/sandbox898315096/main.go:15 +0x60

Can someone explain the difference?

like image 886
Nick Lee Avatar asked Sep 01 '26 11:09

Nick Lee


1 Answers

It is because recover will be nil if not directly called by deferred function

Here is excerpt from golang spec

The return value of recover is nil if any of the following conditions holds:

  1. panic's argument was nil;
  2. the goroutine is not panicking;
  3. recover was not called directly by a deferred function.

For more info check the full spec here

like image 157
Shettyh Avatar answered Sep 03 '26 06:09

Shettyh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!