基于panic
recover
实现类似try...catch...
的异常处理机制。
tr := exception.New()
tr.Try(
func() {
n1, err := strconv.Atoi("123a")
tr.Throw(err)
n2, err := strconv.Atoi("0")
tr.Throw(err)
res := n1 / n2
fmt.Println(res)
},
).Catch(
func(e exception.Exception) {
fmt.Println("exception:", e)
},
)
output
exception: strconv.ParseInt: parsing "123a": invalid syntax
change 123a
to 123
output
exception: runtime error: integer divide by zero