ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
* error: 可预见的错误 * pannic: 不可预见的异常 * pannic 和 defer 打印顺序 : pannic 后终止后续代码执行, 按后进先出顺序打印defer 最后再打印pannic ~~~ package main //https://blog.golang.org/defer-panic-and-recover import "fmt" func main() { f() fmt.Println("Returned normally from f.") } func f() { defer func() { if r := recover(); r != nil { fmt.Println("Recovered in f", r) } }() fmt.Println("Calling g.") g(0) fmt.Println("Returned normally from g.") } func g(i int) { if i > 3 { fmt.Println("Panicking!") panic(1) //panic 中止了当前函数,逐级回溯直至在 defer 中遇到 recover 后恢复。 } defer fmt.Println("Defer in g", i) fmt.Println("Printing in g", i) g(i + 1) } ~~~