From: Rob Pike Date: Tue, 12 Mar 2013 21:28:16 +0000 (-0700) Subject: spec: rewrite the description of panic and recover. X-Git-Tag: go1.1rc2~545 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=c34050fd535113b6e1ed6fc00c9228bbd7e112db;p=gostls13.git spec: rewrite the description of panic and recover. The old description was misleading and inaccurate. Fixes #4774. R=iant, rsc, gri CC=golang-dev https://golang.org/cl/7761044 --- diff --git a/doc/go_spec.html b/doc/go_spec.html index 53f079a2f7..bf96322517 100644 --- a/doc/go_spec.html +++ b/doc/go_spec.html @@ -1,6 +1,6 @@ @@ -5270,14 +5270,14 @@ func recover() interface{}

-A panic call in a function F terminates the execution -of F. +While executing a function F, +an explicit call to panic or a run-time panic +terminates the execution of F. Any functions deferred by F -are executed before F returns to its caller. To the caller, -the call of F then behaves itself like a call to panic, -terminating its own execution and running deferred functions in the same manner. -This continues until all functions in the goroutine have ceased execution, -in reverse order. At that point, the program is terminated and the error +are then executed as usual. +Next, any deferred functions run by F's caller are run, +and so on up to any deferred by the top-level function in the executing goroutine. +At that point, the program is terminated and the error condition is reported, including the value of the argument to panic. This termination sequence is called panicking.

@@ -5290,15 +5290,34 @@ panic(Error("cannot parse"))

The recover function allows a program to manage behavior -of a panicking goroutine. Executing a recover call -inside a deferred function (but not any function called by it) stops -the panicking sequence by restoring normal execution, and retrieves -the error value passed to the call of panic. If -recover is called outside the deferred function it will -not stop a panicking sequence. In this case, or when the goroutine -is not panicking, or if the argument supplied to panic -was nil, recover returns nil. +of a panicking goroutine. +Suppose a function G defers a function D that calls +recover and a panic occurs in a function on the same goroutine in which G +is executing. +When the running of deferred functions reaches D, +the return value of D's call to recover will be the value passed to the call of panic. +If D returns normally, without starting a new +panic, the panicking sequence stops. In that case, +the state of functions called between G and the call to panic +is discarded, and normal execution resumes. +Any functions deferred by G before D are then run and G's +execution terminates by returning to its caller. +

+ +

+The return value of recover is nil if any of the following conditions holds:

+

The protect function in the example below invokes