头图

interview questions

This is the first article in the Go Quiz series about recover , which mainly examines under what circumstances the recover function can capture panic.

 func main() {
  fmt.Print("1 ")
  defer recover()
  fmt.Print("2 ")
  var a []int
  _ = a[0]
  fmt.Print("3 ")
}
  • A: 1 2
  • B: 1 2 3
  • C: 1 2 panic
  • D: 1 2 3 panic
  • E: compile error

This question has the following points:

  • When is the defer statement executed?
  • When can recover catch panic

You can pause for a while and think about the answer.

Topic Analysis

Let's review the basics of recover and panic first:

recover is a built-in function of Go that can catch panic exceptions, but recover must be used in conjunction with defer to take effect.

If the current goroutine triggers a panic, you can call the recover function at an appropriate point in the code to catch the exception and let the program continue normal execution instead of terminating abnormally.

If no panic is generated during the normal execution of the program, then calling the recover function will return nil, other than that, there is no other effect.

recover returns nil in the following cases

  • The argument to panic is nil. In this case, after recover capture, the return value obtained is also nil.
  • goroutines do not panic. If there is no panic, then of course the recovery will be nil.
  • recover is not directly called and executed in the defer function . At this time, panic cannot be captured, and the return value of the recover function is nil.

The official documentation is as follows:

The recover built-in function allows a program to manage behavior of a panicking goroutine. 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. In this case, or when the goroutine is not panicking, or if the argument supplied to panic was nil, recover returns nil. Thus the return value from recover reports whether the goroutine is panicking.

recover must be executed inside the defer function. If revcover is executed directly outside the defer function, panic will not be captured.

For this question, defer recover() this line of code recover is not executed in the defer function, but directly followed by defer.

Therefore, the recovery of this topic cannot capture the panic. Therefore, the answer to this question is C, and the output of the program is as follows:

 1 2 panic: runtime error: index out of range [0] with length 0

goroutine 1 [running]:
main.main()
        /Users/xxx/quiz2.go:11 +0x106
exit status 2

thinking questions

I will leave a few related thinking questions for everyone. If you want to know the answer, you can send a message to my vx public recover to get the answer and problem analysis.

Topic 1

 package main

import "fmt"

func main() {
    defer func() { fmt.Println(recover()) }()
    defer panic(1)
    panic(2)
}
  • A: 1
  • B: 2
  • C: print 1 first, then panic
  • D: print 2 first, then panic

Topic 2

 func main() {
      fmt.Println(recover())
    panic(1)
}
  • A: throw panic
  • B: print 1

topic 3

 func main() {
    defer fmt.Println(recover())
    panic(1)
}
  • A: throw panic
  • B: print 1

Topic 4

 func main() {
    defer func() {
      func() { fmt.Println(recover()) }()
    }()
    panic(1)
}
  • A: throw panic
  • B: print 1

topic 5

 func main() {
    defer func() {
      fmt.Println(recover())
    }()
    panic(1)
}
  • A: throw panic
  • B: print 1

Recommended reading

open source address

Articles and sample code are open sourced on GitHub: Beginner, Intermediate, and Advanced Tutorials in Go .

Official account: coding advanced. Follow the official account to get the latest Go interview questions and technology stacks.

Personal website: Jincheng's Blog .

Zhihu: Wuji .

Welfare

I have compiled a back-end development learning material package for you, including programming language entry to advanced knowledge (Go, C++, Python), back-end development technology stack, interview questions, etc.

Follow the official account "coding advanced", send a message backend to receive a gift package, this information will be updated from time to time, and add information that I think is valuable. You can also send a message "join the group " to communicate and learn with your peers and answer questions.

References


coding进阶
124 声望18 粉丝