--------------------------流程控制可达----------------------------- package main import "fmt" func explode() { // Cause a panic. panic("WRONG") } func throwPanic(f func()) { defer func() { if err := recover(); err != nil { // Handle our error. fmt.Println("FIX") fmt.Println("ERR", err) } }() f() fmt.Println(" finish") } func main() { throwPanic(explode) fmt.Println(" reach here ")//可达 } --------------------------流程控制不可达----------------------------- package main import "fmt" func explode() { // Cause a panic. panic("WRONG") } func main() { // Handle errors in defer func with recover. defer func() { if err := recover(); err != nil { // Handle our error. fmt.Println("FIX") fmt.Println("ERR", err) } }() // This causes an error. explode() fmt.Println(" reach here ")// 达不到 }
时间: 2024-11-05 15:29:54