func test() {
defer func() {
err := recover() //recover 捕获 错误和异常处理
if err != nil { //返回捕获到的错误
fmt.Println("err=", err)
}
}()
res1 := 10
res2 := 0
res3 := res1 / res2
fmt.Printf("%v", res3)
}
func main(){
test()
fmt.Println("123")
}
众所周知 十除 0 会报错 但是要想执行不了test()但是下面的 123还要输出出来 就是用的 错误捕捉 recover