NOTE

1.9 panic and recover

What panic and recover are, why they are needed, how to use them, and the runtime calls behind them.

GoCreated Updated 1 min readhistorical

This is a historical learning note and may contain outdated or incomplete understanding.

1. What They Are

  • panic is a built-in function that can stop the normal flow.
  • recover is a built-in function that can catch a panic. recover is only useful inside a deferred function.

2. Why They Are Needed

  • panic is generally used for unrecoverable errors that prevent the program from continuing.
  • recover is used to prevent the entire process from going down after a panic; in that situation, recover is needed to restore execution.

3. Usage

func TestPanic1(t *testing.T) {
	defer func() {
		if err := recover(); err != nil {
			fmt.Println("program panic. err=", err)
		}
	}()
	panic("program crashed")
}

// output
program panic. err= program crashed

4. Implementation

func TestPanic1(t *testing.T) {
	defer func() {
		if err := recover(); err != nil {
			fmt.Println("program panic. err=", err)
		}
	}()
	panic("program crashed")
}

// output
program panic. err= program crashed
  • go tool compile -S
0x0024 00036 (panic1_test.go:9)	PCDATA	$0, $0
0x0024 00036 (panic1_test.go:9)	PCDATA	$1, $0
0x0024 00036 (panic1_test.go:9)	MOVL	$0, ""..autotmp_2+16(SP)
0x002c 00044 (panic1_test.go:9)	PCDATA	$0, $1
0x002c 00044 (panic1_test.go:9)	LEAQ	"".TestPanic1.func1·f(SB), AX
0x0033 00051 (panic1_test.go:9)	PCDATA	$0, $0
0x0033 00051 (panic1_test.go:9)	MOVQ	AX, ""..autotmp_2+40(SP)
0x0038 00056 (panic1_test.go:9)	PCDATA	$0, $1
0x0038 00056 (panic1_test.go:9)	LEAQ	""..autotmp_2+16(SP), AX
0x003d 00061 (panic1_test.go:9)	PCDATA	$0, $0
0x003d 00061 (panic1_test.go:9)	MOVQ	AX, (SP)
0x0041 00065 (panic1_test.go:9)	CALL	runtime.deferprocStack(SB)
0x0046 00070 (panic1_test.go:9)	TESTL	AX, AX
0x0048 00072 (panic1_test.go:9)	JNE	102
0x004a 00074 (panic1_test.go:14)	PCDATA	$0, $1
0x004a 00074 (panic1_test.go:14)	LEAQ	type.string(SB), AX
0x0051 00081 (panic1_test.go:14)	PCDATA	$0, $0
0x0051 00081 (panic1_test.go:14)	MOVQ	AX, (SP)
0x0055 00085 (panic1_test.go:14)	PCDATA	$0, $1
0x0055 00085 (panic1_test.go:14)	LEAQ	""..stmp_0(SB), AX
0x005c 00092 (panic1_test.go:14)	PCDATA	$0, $0
0x005c 00092 (panic1_test.go:14)	MOVQ	AX, 8(SP)
0x0061 00097 (panic1_test.go:14)	CALL	runtime.gopanic(SB)
0x0066 00102 (panic1_test.go:9)	XCHGL	AX, AX
0x0067 00103 (panic1_test.go:9)	CALL	runtime.deferreturn(SB)
0x006c 00108 (panic1_test.go:9)	MOVQ	72(SP), BP
0x0071 00113 (panic1_test.go:9)	ADDQ	$80, SP
0x0075 00117 (panic1_test.go:9)	RET
0x0028 00040 (panic1_test.go:10)	PCDATA	$0, $1
0x0028 00040 (panic1_test.go:10)	PCDATA	$1, $0
0x0028 00040 (panic1_test.go:10)	LEAQ	""..fp+112(SP), AX
0x002d 00045 (panic1_test.go:10)	PCDATA	$0, $0
0x002d 00045 (panic1_test.go:10)	MOVQ	AX, (SP)
0x0031 00049 (panic1_test.go:10)	CALL	runtime.gorecover(SB)
0x0036 00054 (panic1_test.go:10)	PCDATA	$0, $1
0x0036 00054 (panic1_test.go:10)	MOVQ	16(SP), AX
0x003b 00059 (panic1_test.go:10)	MOVQ	8(SP), CX
0x0040 00064 (panic1_test.go:10)	TESTQ	CX, CX
0x0043 00067 (panic1_test.go:10)	JNE	79
  • panic calls runtime.gopanic(SB).
  • recover calls runtime.gorecover(SB).

5. References

Discussion

Sign in with GitHub to comment. Discussions are stored as GitHub Issues.View on GitHub