Source file
test/recover1.go
1
2
3
4
5
6
7
8
9
10 package main
11
12 import "runtime"
13
14 func main() {
15 test1()
16 test2()
17 test3()
18 test4()
19 test5()
20 test6()
21 test7()
22 }
23
24 func die() {
25 runtime.Breakpoint()
26 }
27
28 func mustRecover(x interface{}) {
29 mustNotRecover()
30 v := recover()
31 if v == nil {
32 println("missing recover")
33 die()
34 }
35 if v != x {
36 println("wrong value", v, x)
37 die()
38 }
39
40
41 v = recover()
42 if v != nil {
43 println("recover didn't recover")
44 die()
45 }
46 }
47
48 func mustNotRecover() {
49 v := recover()
50 if v != nil {
51 println("spurious recover")
52 die()
53 }
54 }
55
56 func withoutRecover() {
57 mustNotRecover()
58 }
59
60 func test1() {
61
62 defer mustRecover(1)
63 defer func() {
64 defer mustRecover(2)
65 panic(2)
66 }()
67 panic(1)
68 }
69
70 func test2() {
71
72 defer mustNotRecover()
73 defer func() {
74 v := recover()
75 if v == nil || v.(int) != 2 {
76 println("wrong value", v, 2)
77 die()
78 }
79 defer mustRecover(3)
80 panic(3)
81 }()
82 panic(2)
83 }
84
85 func test3() {
86
87 defer mustNotRecover()
88 defer func() {
89 recover()
90 defer mustRecover(3)
91 panic(3)
92 }()
93 panic(2)
94 }
95
96 func test4() {
97
98 defer mustNotRecover()
99 defer func() {
100 recover()
101 }()
102 panic(4)
103 }
104
105 func test5() {
106
107 defer mustNotRecover()
108 defer func() {
109 defer recover()
110 }()
111 panic(5)
112 }
113
114 func test6() {
115
116
117 defer mustNotRecover()
118 defer func() {
119 defer recover()
120 defer mustRecover(3)
121 panic(3)
122 }()
123 panic(2)
124 }
125
126 func test7() {
127
128
129
130
131
132
133
134 defer mustRecover(2)
135 defer func() {
136 defer mustRecover(3)
137 defer recover()
138 panic(3)
139 }()
140 panic(2)
141 }
142
View as plain text