3 // Copyright 2010 The Go Authors. All rights reserved.
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file.
7 // Test of recover during recursive panics.
25 runtime.Breakpoint() // can't depend on panic
28 func mustRecover(x interface{}) {
29 mustNotRecover() // because it's not a defer call
32 println("missing recover")
33 die() // panic is useless here
36 println("wrong value", v, x)
40 // the value should be gone now regardless
43 println("recover didn't recover")
48 func mustNotRecover() {
51 println("spurious recover")
56 func withoutRecover() {
57 mustNotRecover() // because it's a sub-call
61 // Easy nested recursive panic.
72 defer mustNotRecover()
75 if v == nil || v.(int) != 2 {
76 println("wrong value", v, 2)
86 // Sequential panic - like test2 but less picky.
87 defer mustNotRecover()
98 defer mustNotRecover()
106 // Single panic but recover called via defer
107 defer mustNotRecover()
116 // Like test3, but changed recover to defer (same change as test4 → test5).
117 defer mustNotRecover()
119 defer recover() // like a normal call from this func; runs because mustRecover stops the panic
127 // Like test6, but swapped defer order.
128 // The recover in "defer recover()" is now a no-op,
129 // because it runs called from panic, not from the func,
130 // and therefore cannot see the panic of 2.
131 // (Alternately, it cannot see the panic of 2 because
132 // there is an active panic of 3. And it cannot see the
133 // panic of 3 because it is at the wrong level (too high on the stack).)
137 defer recover() // now a no-op, unlike in test6.