Hǎiliàng
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
Hǎiliàng
Hǎiliàng
http://golang.org/ref/spec#Handling_panics"When a function F calls panic, normal execution of F stops immediately. Any functions whose execution was deferred by the invocation of F are run in the usual way, and then F returns to its caller. To the caller, F then behaves like a call to panic, terminating its own execution and running deferred functions. This continues until all functions in the goroutine have ceased execution, in reverse order. 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."
I don't know what exact behaviour you are seeing but the spec clearly states that panics run all deferred functions for that function. The only way a defer in a function wouldn't be run is if the panic happens before the defer call. So for example if I do thisfunc f() {
f, err := os.Open(...)panic("AAAAAAhhhhh")if err != nil {defer f.Close() // this defer won't run because the the function panics before the defer is scheduled.}}This is programmer error though since they are panicking before scheduling any cleanup to happen.recover isn't a magical syntax that forces it deferred's to run it works because deferred calls run when you panic.
On Thursday, February 21, 2013 1:45:28 PM UTC-6, Jeremy Wall wrote:http://golang.org/ref/spec#Handling_panics"When a function F calls panic, normal execution of F stops immediately. Any functions whose execution was deferred by the invocation of F are run in the usual way, and then F returns to its caller. To the caller, F then behaves like a call to panic, terminating its own execution and running deferred functions. This continues until all functions in the goroutine have ceased execution, in reverse order. 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."
I don't know what exact behaviour you are seeing but the spec clearly states that panics run all deferred functions for that function. The only way a defer in a function wouldn't be run is if the panic happens before the defer call. So for example if I do thisfunc f() {
f, err := os.Open(...)panic("AAAAAAhhhhh")if err != nil {defer f.Close() // this defer won't run because the the function panics before the defer is scheduled.}}This is programmer error though since they are panicking before scheduling any cleanup to happen.recover isn't a magical syntax that forces it deferred's to run it works because deferred calls run when you panic.
http://play.golang.org/p/QQyVVOFXz4 this illustrates his problem, only one of these 10 defers gets run.
Now if instead of panic()'ing there was error detection all of the defers could run.
Or the panic could get recovered and running could continue as normal.
On Thursday, February 21, 2013 1:56:05 PM UTC-6, bryanturley wrote:
On Thursday, February 21, 2013 1:45:28 PM UTC-6, Jeremy Wall wrote:http://golang.org/ref/spec#Handling_panics
"When a function F calls panic, normal execution of F stops immediately. Any functions whose execution was deferred by the invocation of F are run in the usual way, and then F returns to its caller. To the caller, F then behaves like a call to panic, terminating its own execution and running deferred functions. This continues until all functions in the goroutine have ceased execution, in reverse order. 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."
I don't know what exact behaviour you are seeing but the spec clearly states that panics run all deferred functions for that function. The only way a defer in a function wouldn't be run is if the panic happens before the defer call. So for example if I do thisfunc f() {
f, err := os.Open(...)panic("AAAAAAhhhhh")if err != nil {defer f.Close() // this defer won't run because the the function panics before the defer is scheduled.}}This is programmer error though since they are panicking before scheduling any cleanup to happen.recover isn't a magical syntax that forces it deferred's to run it works because deferred calls run when you panic.
http://play.golang.org/p/QQyVVOFXz4 this illustrates his problem, only one of these 10 defers gets run.
Now if instead of panic()'ing there was error detection all of the defers could run.
Or the panic could get recovered and running could continue as normal.
On Thu, Feb 21, 2013 at 1:57 PM, bryanturley <bryan...@gmail.com> wrote:
On Thursday, February 21, 2013 1:56:05 PM UTC-6, bryanturley wrote:
On Thursday, February 21, 2013 1:45:28 PM UTC-6, Jeremy Wall wrote:http://golang.org/ref/spec#Handling_panics
"When a function F calls panic, normal execution of F stops immediately. Any functions whose execution was deferred by the invocation of F are run in the usual way, and then F returns to its caller. To the caller, F then behaves like a call to panic, terminating its own execution and running deferred functions. This continues until all functions in the goroutine have ceased execution, in reverse order. 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."
I don't know what exact behaviour you are seeing but the spec clearly states that panics run all deferred functions for that function. The only way a defer in a function wouldn't be run is if the panic happens before the defer call. So for example if I do thisfunc f() {
f, err := os.Open(...)panic("AAAAAAhhhhh")if err != nil {defer f.Close() // this defer won't run because the the function panics before the defer is scheduled.}}This is programmer error though since they are panicking before scheduling any cleanup to happen.recover isn't a magical syntax that forces it deferred's to run it works because deferred calls run when you panic.
http://play.golang.org/p/QQyVVOFXz4 this illustrates his problem, only one of these 10 defers gets run.
Now if instead of panic()'ing there was error detection all of the defers could run.
Or the panic could get recovered and running could continue as normal.Since the goroutines never return or panic themselves then the defer never runs. I'm actually not sure if that's a bug or not. The spec doesn't directly address this by my reading :-p I wonder if this is what the Go authors intended?
If you think of panic() on the level of a segfault or other major kill signal it makes sense.
If you think of it as an exception try/catch mechanism perhaps not?