FAQ
I assume, I can use named functions for defer (most examples are self
invoking anonymous fn). I use it like

func ErrorHandler(w http.ResponseWriter, r *http.Request) {
if rec := recover(); rec != nil {
if _, ok := rec.(runtime.Error); ok {
println("========= runtime error ==========")
panic(rec)
}
if err, ok := rec.(error); ok {
println("========= Error ==========")
} else {
println("========= Non error ==========")
}
} else {
println("========= Recover without any value? ==========")
}
}


and uses as

defer ErrorHandler(w, r)
in http handlers.

Now, I am getting the message "========= Recover without any value?
=========="

I am not sure how/where this is happening. I looked in my code to see any
explicit panic() call without an error (like panic(err)), but I don't see
any.

How can I find out what is causing the defer

thanks.


--
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+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

  • Steven Blenkinsop at Nov 5, 2013 at 9:39 pm
    The deferred function will be executed regardless of whether there is a
    panic or not. In that case, recover will return nil. This signals that your
    code isn't panicking (unless someone passed a nil into panic, but who would
    do that?). Your program is behaving correctly. Just remove the else block,
    since you don't need to do anything in that case.

    On Tue, Nov 5, 2013 at 4:34 PM, bsr wrote:

    I assume, I can use named functions for defer (most examples are self
    invoking anonymous fn). I use it like

    func ErrorHandler(w http.ResponseWriter, r *http.Request) {
    if rec := recover(); rec != nil {
    if _, ok := rec.(runtime.Error); ok {
    println("========= runtime error ==========")
    panic(rec)
    }
    if err, ok := rec.(error); ok {
    println("========= Error ==========")
    } else {
    println("========= Non error ==========")
    }
    } else {
    println("========= Recover without any value? ==========")
    }
    }


    and uses as

    defer ErrorHandler(w, r)
    in http handlers.

    Now, I am getting the message "========= Recover without any value?
    =========="

    I am not sure how/where this is happening. I looked in my code to see any
    explicit panic() call without an error (like panic(err)), but I don't see
    any.

    How can I find out what is causing the defer

    thanks.


    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Jan Mercl at Nov 5, 2013 at 9:39 pm

    On Tue, Nov 5, 2013 at 10:34 PM, bsr wrote:
    How can I find out what is causing the defer
    If a deferred function was not called from a panic then it was invoked
    by returning from the function in which the defers statement appears.
    I think it should be also possible to verify that using eg.
    debug.Stack().

    -j

    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Bsr at Nov 5, 2013 at 10:07 pm
    Thanks for the help. Never noticed defer got called in success cases.
    On Tuesday, November 5, 2013 4:39:11 PM UTC-5, Jan Mercl wrote:

    On Tue, Nov 5, 2013 at 10:34 PM, bsr <bsr...@gmail.com <javascript:>>
    wrote:
    How can I find out what is causing the defer
    If a deferred function was not called from a panic then it was invoked
    by returning from the function in which the defers statement appears.
    I think it should be also possible to verify that using eg.
    debug.Stack().

    -j
    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Babu Sreekanth at Nov 5, 2013 at 11:58 pm
    . Never knew it. thanks for the help
    On Tuesday, November 5, 2013 4:39:11 PM UTC-5, Jan Mercl wrote:

    On Tue, Nov 5, 2013 at 10:34 PM, bsr <bsr...@gmail.com <javascript:>>
    wrote:
    How can I find out what is causing the defer
    If a deferred function was not called from a panic then it was invoked
    by returning from the function in which the defers statement appears.
    I think it should be also possible to verify that using eg.
    debug.Stack().

    -j
    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Rob Pike at Nov 6, 2013 at 12:15 am
    If that didn't work, how would this work?

    func foo() {
        mu.Lock()
        defer mu.Unlock()
        doSomethingWithData()
    }

    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedNov 5, '13 at 9:34p
activeNov 6, '13 at 12:15a
posts6
users5
websitegolang.org

People

Translate

site design / logo © 2021 Grokbase