//You could use defer-recovery to handle all possible panic's
func handelPanic(){
if recoverPanic := recovery();recoverPanic != nil{
fmt.Println("Panic is being recovered...")
//Call function which you want to restart after panic is being recovered
}
}
func main() {
defer handlePanic()
//some code that needs to check err
if err != nil{
panic(err)
}
}
1条答案
按热度按时间yyhrrdl81#
上面下面的代码将简单地处理所有的恐慌,如果恢复使用在主函数内推迟。