github.com/ks888/tgo@v0.0.0-20190130135156-80bf89407292/testutils/testdata/panic.go (about) 1 package main 2 3 import ( 4 "fmt" 5 ) 6 7 func main() { 8 f() 9 fmt.Println("Returned normally from f.") 10 } 11 12 func f() { 13 defer catch() 14 fmt.Println("Calling g.") 15 g(0) 16 fmt.Println("Returned normally from g.") 17 } 18 19 func g(i int) { 20 if i > 1 { 21 throw(i) 22 } 23 defer through(i) 24 fmt.Println("Printing in g", i) 25 g(i + 1) 26 } 27 28 func throw(i int) { 29 fmt.Println("Panicking!") 30 panic(fmt.Sprintf("%v", i)) 31 } 32 33 func through(i int) { 34 // to check the case in which call 'defer' while handling panic. 35 insideThrough := func() { 36 fmt.Println("through", i) 37 } 38 defer insideThrough() 39 } 40 41 func catch() { 42 if r := recover(); r != nil { 43 fmt.Println("Recovered in f", r) 44 } 45 }