如何处理golang程序中出现的panic

本文介绍了如何在Golang程序中处理panic,通过defer和recover实现类似try-catch的效果。文章通过两个实例讲解:一是代码片段出现panic后的捕获处理,二是goroutine循环执行时如何在panic后继续执行。

如何处理golang程序中出现的panic

本文章主要通过举例两种情况来说明如何通过defer和recover处理panic,让go实现类似于其它语言的try catch一样的方法
第一种是在执行一个代码片段中出现panic,如何在panic之后如何捕获panic信息
第二种是在一个goroutine的循环执行中,如何保证在发生panic后还可以继续执行

翠花 上代码

第一种情况,在代码中出现了panic之后如何优雅的退出

/*
main函数在调用division方法时除数传入了0,这个时候会造成panic,defer中则为处理panic的逻辑
*/
func main() {
	//这儿是为了处理panic
	defer func() {
		if err := recover(); err != nil {
			fmt.Println("panic触发错误", err)
			fmt.Println("捕获到panic后的处理可以写在这儿")
		}
	}()
	res, err := division(4, 0)
	//panic后这儿不会处理
	fmt.Printf("结果为%d,error为:%v", res, err)
}

/*panic处理*/
func division(a, b int) (c int, err error) {
	c = a / b
	return
}

第二种情况是在循环处理任务时出现panic,如何进行跳过该次错误,继续执行


func main() {
	stopCh := make(chan struct{})
	panicContinue(stopCh)
	//sleep10秒
	time.Sleep(time.Second * 10)
	//给stopCH塞入一个值,告诉panicContinue,你可以退出了
	stopCh <- struct{}{}
	//退出
	fmt.Println("finish")
}


func panicContinue(ch chan struct{}) {
	//做一个定时器,每秒触发一次
	timeTicker := time.NewTicker(time.Second)
	for {
		select {
		case <-timeTicker.C:
			go func() {
				defer func() {
					if err := recover(); err != nil {
						fmt.Println("发现错误。。。。")
						fmt.Println(err)
					}
				}()
				proc()
			}()
		case <-ch:
			//退出
			break
		default:
		}
	}
}

func proc() {
	fmt.Printf("当前时间为:%v\n", time.Now().Format("2006-01-02 15:04:05"))
	panic("i am panic")
}
总结

当程序出现panic后,会先执行defer,这时候我们就可以在defer中去捕获和处理panic,recover方法时官方提供的处理panic的方法,源码如下(go的注释写的是真的好)

// 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 panic(v any)

// 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.
func recover() any
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值