/usr/share/go-1.8/test/recover1.go is in golang-1.8-src 1.8.3-2ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 | // run
// Copyright 2010 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Test of recover during recursive panics.
// Here be dragons.
package main
import "runtime"
func main() {
test1()
test2()
test3()
test4()
test5()
test6()
test7()
}
func die() {
runtime.Breakpoint() // can't depend on panic
}
func mustRecover(x interface{}) {
mustNotRecover() // because it's not a defer call
v := recover()
if v == nil {
println("missing recover")
die() // panic is useless here
}
if v != x {
println("wrong value", v, x)
die()
}
// the value should be gone now regardless
v = recover()
if v != nil {
println("recover didn't recover")
die()
}
}
func mustNotRecover() {
v := recover()
if v != nil {
println("spurious recover")
die()
}
}
func withoutRecover() {
mustNotRecover() // because it's a sub-call
}
func test1() {
// Easy nested recursive panic.
defer mustRecover(1)
defer func() {
defer mustRecover(2)
panic(2)
}()
panic(1)
}
func test2() {
// Sequential panic.
defer mustNotRecover()
defer func() {
v := recover()
if v == nil || v.(int) != 2 {
println("wrong value", v, 2)
die()
}
defer mustRecover(3)
panic(3)
}()
panic(2)
}
func test3() {
// Sequential panic - like test2 but less picky.
defer mustNotRecover()
defer func() {
recover()
defer mustRecover(3)
panic(3)
}()
panic(2)
}
func test4() {
// Single panic.
defer mustNotRecover()
defer func() {
recover()
}()
panic(4)
}
func test5() {
// Single panic but recover called via defer
defer mustNotRecover()
defer func() {
defer recover()
}()
panic(5)
}
func test6() {
// Sequential panic.
// Like test3, but changed recover to defer (same change as test4 → test5).
defer mustNotRecover()
defer func() {
defer recover() // like a normal call from this func; runs because mustRecover stops the panic
defer mustRecover(3)
panic(3)
}()
panic(2)
}
func test7() {
// Like test6, but swapped defer order.
// The recover in "defer recover()" is now a no-op,
// because it runs called from panic, not from the func,
// and therefore cannot see the panic of 2.
// (Alternately, it cannot see the panic of 2 because
// there is an active panic of 3. And it cannot see the
// panic of 3 because it is at the wrong level (too high on the stack).)
defer mustRecover(2)
defer func() {
defer mustRecover(3)
defer recover() // now a no-op, unlike in test6.
panic(3)
}()
panic(2)
}
|