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() }