异常处理
panic 抛出异常
recover 捕获异常
func receivePanic() {
defer func() {
message := recover()
fmt.Println("panic message:",message)
}()
panic("I am panic")
}
换种写法
func receivePanic() {
defer coverPanic()
panic(errors.New(" this is error "))
}
func coverPanic() {
message := recover()
switch message.(type) {
case string:
fmt.Println("string message: ",message)
case error:
fmt.Println("error message:",message)
default:
fmt.Println("unknown panic",message)
}
}