golang - recover 使用笔记

本文介绍了Golang中panic机制及其处理方法,重点讲解了recover函数的作用,如何在函数级别捕获异常,以及在协程环境下的正确使用,以防止程序因panic而崩溃。


作用:程序报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  


 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值