On Jan 22, 2014, at 12:13 PM, Jan Mercl <
0xj...@gmail.com> wrote:
> On Wed, Jan 22, 2014 at 9:04 PM, Brendan Tracey
> <
tracey....@gmail.com> wrote:
>> I just had an encounter with issue 6546, and I was curious about the
>> rationale for panic(nil) returning nil to recover. It would seem to me that
>> the desired behavior is "the recover error is non-nil if the code panicked".
>> panic(nil) does in fact panic, so why not signal so to recover?
>
> See
http://golang.org/ref/spec#Handling_panics
>
> """"
> The return value of recover is nil if any of the following conditions holds:
>
> panic's argument was nil;
> the goroutine is not panicking;
> recover was not called directly by a deferred function.
> """"
>
> IOW, the behavior of panic(nil) vs recover() result is strictly
> specified - and thus expected. Also mind the Go 1 compatibility
> promise.
>
I agree that that’s what the spec says, and that changing so would break Go 1, I was intending to ask about the purpose of the behavior
> BTW: The specified behavior of panic(nil) is _very_ useful. One can
> also think about it as: recover simply returns what the argument to a
> possible panic was, no special cases.
I guess that’s nice, but it still seems like one would rather signal the panic. For example, in the example code in that section of the spec, if one wanted to be more careful, one might say
"
if x := recover(); x != nil{
log.Printf(“run time panic with a non-nil argument: %v,”, x)
}
“
Are there cases in an honest program where calling panic(nil) (so that the error “hides” from recover) is desirable?