go异常处理

panic、recover

package main

import (
	"fmt"
)

func tryRecover() {
	defer func() {
		r := recover()
		if err, ok := r.(error); ok {
			fmt.Println("error occurred:", err)
		} else {
			panic(r)
		}
	}()
	a := 5
	b := 0
	c := a / b
	fmt.Println(c)
}

func main() {
	tryRecover()
}

  

posted @ 2018-08-23 10:50  hongpeng0209  阅读(108)  评论(0编辑  收藏  举报