mirror of
https://github.com/unknwon/the-way-to-go_ZH_CN.git
synced 2025-08-11 22:53:43 +08:00
36 lines
456 B
Go
Executable File
36 lines
456 B
Go
Executable File
// recover_divbyzero.go
|
|
package main
|
|
|
|
import (
|
|
"fmt"
|
|
)
|
|
|
|
func badCall() {
|
|
a, b := 10, 0
|
|
n := a / b
|
|
fmt.Println(n)
|
|
}
|
|
|
|
func test() {
|
|
defer func() {
|
|
if e := recover(); e != nil {
|
|
fmt.Printf("Panicing %s\r\n", e)
|
|
}
|
|
|
|
}()
|
|
badCall()
|
|
fmt.Printf("After bad call\r\n")
|
|
}
|
|
|
|
func main() {
|
|
fmt.Printf("Calling test\r\n")
|
|
test()
|
|
fmt.Printf("Test completed\r\n")
|
|
}
|
|
|
|
/* Output:
|
|
Calling test
|
|
Panicing runtime error: integer divide by zero
|
|
Test completed
|
|
*/
|