recover()和 panic(v interface{})内建函数
1、所属包:builtin (地址:https://golang.google.cn/pkg/builtin/)
2、内建函数区别
func recover() interface{}
func panic(v interface{})
func panic
func panic(v interface{})
The panic built-in function stops normal execution of the current goroutine. When a function F calls panic, normal execution of F stops immediately. Any functions whose execution was deferred by F are run in the usual way, and then F returns to its caller. To the caller G, the invocation of F then behaves like a call to panic, terminating G's execution and running any deferred functions. This continues until all functions in the executing goroutine have stopped, in reverse order. At that point, the program is terminated with a non-zero exit code. This termination sequence is called panicking and can be controlled by the built-in function recover.
func recover
func recover() interface{}
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.
panic:
1、内建函数
2、假如函数F中书写了panic语句,会终止其后要执行的代码,在panic所在函数F内如果存在要执行的defer函数列表,按照defer的逆序执行
3、返回函数F的调用者G,在G中,调用函数F语句之后的代码不会执行,假如函数G中存在要执行的defer函数列表,按照defer的逆序执行
4、直到goroutine整个退出,并报告错误
recover:
1、内建函数
2、用来控制一个goroutine的panicking行为,捕获panic,从而影响应用的行为
3、一般的调用建议
a). 在defer函数中,通过recever来终止一个gojroutine的panicking过程,从而恢复正常代码的执行
b). 可以获取通过panic传递的error
举例:
1 func (this *testBuiltin) BuiltinFun() { 2 3 //\n换行符,Println会换行 4 fmt.Println("我要认真学习 go语言\n为了美好的生活") 5 6 //测试convert内建函数 7 deferOfCaller := func() { //定义函数变量 8 fmt.Println("this defer of BuiltinFun") 9 } 10 exit1 := func() { 11 fmt.Println("this prent exit1") 12 } 13 14 exit2 := func() { 15 fmt.Println("this prent exit2") 16 } 17 18 defer exit1() 19 defer exit2() 20 defer deferOfCaller() //defer执行先进后出原则 21 22 fmt.Println("I am parent") 23 this.PanicFun("checkPanicFun") 24 25 fmt.Println("I am parent,I will end !") 26 } 27 28 //测试convert内建函数 29 func (this *testBuiltin) PanicFun(str string) { 30 31 //测试panic内建函数 32 deferOfCaller := func() { 33 if p := recover(); p != nil { 34 if err, ok := p.(error); ok { 35 fmt.Println("recover err:", err) 36 } else { 37 fmt.Println("not recover err") 38 panic(p) 39 } 40 } 41 } 42 exit1 := func() { 43 fmt.Println("this Child exit1") 44 } 45 46 exit2 := func() { 47 fmt.Println("this Child exit2") 48 } 49 50 defer exit1() 51 defer exit2() 52 defer deferOfCaller() //defer执行先进后出原则 53 54 fmt.Println("Child.I.am working") 55 56 //创建一个错误 57 e := fmt.Errorf("make an nomal err") 58 panic(e) 59 60 fmt.Println("Child.I.am working after panic ") 61 }
运行的结果:
1、panic(e)遇到panic后面代码就不在执行
posted on 2020-04-21 14:53 HelloOcean 阅读(495) 评论(0) 收藏 举报
浙公网安备 33010602011771号