go异常处理
import ( "errors" "fmt" ) func main() { num, err := ErrorTest(0, 10) if err != nil { fmt.Println(err) } else { fmt.Println(num) } } func ErrorTest(num2 int, num1 int) (result int, err error) { if num2 == 0 { err = errors.New("num2不能等于0") return } result = num1 / num2 return }
import "fmt" func main() { test(11) } func test(n int) { defer TestRecover() var num [10]int num[n] = 12 fmt.Println(num) fmt.Println("aaaa") } func TestRecover() { //recover() //拦截panic函数异常 fmt.Println(recover()) }