the try {} catche () {}
structure in other languages is usually added under the entry function, and then trhow Exception
is directly added when an exception occurs in the implementation of business logic
so in go, there are two ways, one is:
func A(a string) (res string, err error) {
defer func() {
if err := recover(); err != nil {
fmt.Println(err)
}
}()
res = B(a)
return res, err
}
func B(b string) (res string) {
if b == "a" {
return "hello " + b
} else {
panic("params need a")
}
}
my question is:
- what is the usage scenario of these two methods?
- what are the consequences of abusing panic?