Skip to content

Instantly share code, notes, and snippets.

@go101
Created January 6, 2020 17:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save go101/f88420544485b6702ebca0b63b2127e6 to your computer and use it in GitHub Desktop.
Save go101/f88420544485b6702ebca0b63b2127e6 to your computer and use it in GitHub Desktop.
panic-recover mechanism explanations
Assume "returning phase" has been defined elsewhere in Go specificaiton.
At run time, a fucntion call may call other functions.
This forms a call stack at any time in each goroutine.
We call the top call in the stack as the current call.
Each non-existed call in the stack may associate with at most one panic.
At run time, panics may occur when the current call is being executed.
Panics occur either by calling the builtin panic fucntion or
performing operations that cause runtime panics.
A newly occurring panic will associate with the current call,
and make the current enter its returning phase (if it hasn't entered).
If there has alreay been an unrecoverd older panic associating with
the current call, the older one will disassociate from the current call
before the new association is created. A function call associating with
an unrecovered panic is called a panicking function call.
When a panicking function call exits, its associating unrecovered panic will
re-occur in (and associate with) the caller call of the exited panicking call.
When the entry call of a goroutine exits and it is still panicking,
the whole program crashes.
A new invoked function call doesn't associate with any panics,
whether or not it is a deferred call, whether or not its caller is panikcing.
A call to the builtin recover fucntion tries to disassociate the caller of
the caller of the recover call from the possible associating unrecovered panic.
The recover operation will succeed only if all the following conditions
are satisfied:
1. the associating unrecovered panic does exist.
2. the caller of the recover call is not panicking at that time.
3. the caller of the recover call is a deferred call.
If the recover operation succeeds, it returns the correspoding panic value.
Otherwise, the call to recover will return nil.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment