golang 之recover() 使用笔记

作用:程序报panic时,会使整个程序挂掉,在实际工作中,报panic的地方可能会非常的多,一旦报panic会导致整个服务挂掉,是非常危险的。golang 引用recover()函数来捕获异常,使得即使报panic,也能继续运行下去。

通常写法:

defer func() {

if err := recover(); err !=nil {

fmt.Println(err)

}

}()

作用域:

recover() 只是针对当前函数和以及直接调用的函数可能产生的panic,它无法处理其调用产生的其它协程的panic 举例说明:

1.下面程序,一旦运行test2() 函数会导致panic,程序会立即挂掉

package main

import (

"fmt"

)

func main() {

test1() //输出:this is test 1

test2() //输出:this is test 2 panic: test 2 is panic 直接挂掉

test3()

}

func test1 (){

fmt.Println("this is test 1")

}

func test2 (){

fmt.Println("this is test 2")

panic("test 2 is panic")

}

func test3 (){

fmt.Println("this is test 3")

}

2. 当我们在test2() 函数加入recover()时,程序运行到test2()函数,报panic 错误不会挂掉,程序会继续进行,执行test()3函数。

package main

import (

"fmt"

)

func main() {

test1() //输出:this is test 1

test2() //输出:this is test 2 test 2 is panic

test3() //输出:this is test 3

}

func test1 (){

fmt.Println("this is test 1")

}

func test2 (){

defer func() {

if err := recover(); err !=nil {

fmt.Println(err)

}

}()

fmt.Println("this is test 2")

panic("test 2 is panic")

}

func test3 (){

fmt.Println("this is test 3")

}

3.当我们把 recover() 放在 直接调用的test2()的main 函数之中时,当程序执行到test2函数时,报panic 这时test2()程序中断,程序不会往下执行,而是直接执行defer 中的recover()函数(同时说明,即使程序某个位置报了panic错误,最后也会执行defer),整个程序不会挂掉。

package main

import (

"fmt"

)

func main() {

defer func() {

if err := recover(); err !=nil {

fmt.Println(err)

}

}()

test1() //输出: this is test 1

test2() //输出: this is test 2; test 2 is panic

test3() //不会执行

}

func test1 (){

fmt.Println("this is test 1")

}

func test2 (){

fmt.Println("this is test 2")

panic("test 2 is panic")

}

func test3 (){

fmt.Println("this is test 3")

}

4. 当为test2()开了个go 协程时,程序依然会报panic 导致整个程序挂掉。

package main

import (

"fmt"

)

func main() {

defer func() {

if err := recover(); err !=nil {

fmt.Println(err)

}

}()

test1()

go test2()

test3()

for {

select {

}

}

}

func test1 (){

fmt.Println("this is test 1")

}

func test2 (){

fmt.Println("this is test 2")

panic("test 2 is panic")

}

func test3 (){

fmt.Println("this is test 3")

}

5. 当为test2()开了个协程时,正确的做法是 在recove(),放在test2()里面才不会导致整个程序挂掉。

package main

import (

"fmt"

)

func main() {

test1() // 输出:this is test 1

go test2() // this is test 2; test 2 is panic

test3() //this is test3

for { //不推荐这样写 会造成死锁 此处只是单单为了 演示

select {

}

}

}

func test1 (){

fmt.Println("this is test 1")

}

func test2 (){

defer func() {

if err := recover(); err !=nil {

fmt.Println(err)

}

}()

fmt.Println("this is test 2")

panic("test 2 is panic")

}

func test3 (){

fmt.Println("this is test 3")

}

//输出结果:

// this is test 1

// this is test 3

// this is test 2

// test is panic


梦幻西游高效捕捉珍稀宝宝攻略:掌握独家技巧,提升宝宝获取成功率与品质策略分享
淘宝客服回应闪购崩了:如有相关情况说明访问人数较多