恢复用于从紧急情况或错误情况中重新获得对程序的控制。它停止终止序列并恢复正常执行。从延迟函数中调用。它检索通过panic调用传递的错误值。通常, 它返回nil, 没有其他效果。
go recover()示例
package main
import (
"fmt"
)
func main() {
fmt.Println(SaveDivide(10, 0))
fmt.Println(SaveDivide(10, 10))
}
func SaveDivide(num1, num2 int) int {
defer func() {
fmt.Println(recover())
}()
quotient := num1 / num2
return quotient
}
输出:
runtime error: integer divide by zero
0
<nil>
1
微信公众号
手机浏览(小程序)
Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages:
error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in
/mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line
57
Warning: get_headers(): Failed to enable crypto in
/mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line
57
Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_50036.jpg): failed to open stream: operation failed in
/mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line
57