54

I've just read this post about Panic/Recover in Go and I'm not clear on how this differs from try/catch in other mainstream languages.

7 Answers 7

31

Panic/Recover are function scoped. It's like saying that you're only allowed one try/catch block in each function and the try has to cover the whole function. This makes it really annoying to use Panic/Recover in the same way that java/python/c# etc. use exceptions. This is intentional. This also encourages people to use Panic/Recover in the way that it was designed to be used. You're supposed to recover() from a panic() and then return an error value to the caller.

28

I keep looking at this question trying to think of the best way to answer it. It's easiest to just point to the idiomatic uses for panic/recover as opposed to try/catch &| exceptions in other languages, or the concepts behind those idioms (which can be basically summed up as "exceptions should only occur in truly exceptional circumstances")

But as to what the actual difference is between them? I'll try to summarize as best I can.

One of the main differences compared to try/catch blocks is the way control flows. In a typical try/catch scenario, the code after the catch block will run unless it propagates the error. This is not so with panic/recover. A panic aborts the current function and begins to unwind the stack, running deferred functions (the only place recover does anything) as it encounters them.

Really I'd take that even further: panic/recover is almost nothing like try/catch in the sense that try and catch are (or at least act like) control structures, and panic/recover are not.

This really stems out of the fact that recover is built around the defer mechanism, which (as far as I can tell) is a fairly unique concept in Go.

There are certainly more, which I'll add if I can actuate my thoughts a bit better.

10
  • 2
    Thanks, BTW, defer is very similar to D's scope
    – Motti
    Commented Aug 8, 2010 at 10:23
  • 10
    Here is my take: panic is loosely analogous to throw. And a defer with a recover statement is loosely analogous to catch. The closest analog for a try block is the body of the current function. Commented Feb 13, 2014 at 21:53
  • 1
    Then what is the difference from the usage perspective? Right now it seems to me as if it behaves like a try/catch block with restrictions to span across the whole function (i.e. you can not have multiple blocks like that) and recover is a way to obtain an exception-like object. And with restriction that exceptions do not bobble up outside your scope.
    – Ben Usman
    Commented Sep 6, 2018 at 22:41
  • 2
    This post is highly dogmatic. "A panic aborts the current function and begins to unwind the stack" is what an exception does as well. Panic/recover and throw/catch are semantically different solutions to the same problem
    – Espen
    Commented Feb 2, 2021 at 7:38
  • 2
    @Espen A decade later, I'm sure I'd explain differently (and hopefully better), but the context of that statement was in the context of panic/recover vs throw/catch, not just panic vs throw. Yes both panic and throw begin to unwind the stack, but in go it is not possible to resume execution of the current function, while catch does allow you to resume the current function.
    – cthom06
    Commented Feb 5, 2021 at 19:15
20

I think we all agree that panic is throw, recover is catch, and defer is finally.

The big difference seems that recover goes inside defer. Going back to traditional terms, it lets you decide exactly at which point of your finally you want to bother catching anything, or not at all.

10

defer is a mechanism not only for handling errors but also for doing a comfortable and controlled cleanup. Now panic works like raise() in other languages. With the help of the function recover() you've got the chance to catch this panic while it goes up the call stack. This way it's almost similar to try/catch. But while the latter works on blocks panic/recover works on function level.

Rob Pike about the reason for this solution: "We don't want to encourage the conflation of errors and exceptions that occur in languages such as Java.". Instead of having a large number of different exceptions with an even larger number of usages one should do everything to avoid runtime errors, deliver proper error return values after determination and use panic/recover only if there's no other way.

4

I think the Panic is the same as throw,Recover is the same as catch. The difference is at defer. At the begin, I think defer is the same as finally,but later, I find defer is more flexible than finally. defer can be place at any scope of your function and remember the value of parameter at that moment and also can change the returned return value,the the panic can be at any where after defer. but because the missing of block of try, we can't process the "exception" unless the whole function returned. I don't think this is a disadvantage,maybe GO want to make your method only do one thing, any exception should make this thing can't go on. and since panic must after defer, it make you must process its "exception" before use it.

this is just understanding of myself .

1

"go" is unlike other languages a concurrent language, meaning that parts of the program runs in parallel. This means that if the designers want to have a similar mechanism as catch/throw , the mechanism has to meticulously redefined in this context. That explains the differences between the two mechanisms.

1
  • 1
    Would love to hear an elaboration on this..java has fairly complex concurrency primitives in place as well, how does go's panic/recover/defer handle it differently?
    – Siddhartha
    Commented Aug 15, 2023 at 0:52
0

Panic/Recover is a compromised and simplified version of try/catch. What Panic/Recover can do, try/catch can do, and what try/catch can do, Panic/Recover may have to add a little extra code to accomplish. The reason why go doesn't use try/catch, but invented Panic/Recover, I think that no matter what reason the creator of go said, in fact, it's that try/catch/finally implementation is far more complex than Panic/Recover/defer, go creator doesn't want to run into this trouble, so can only throw the problem to the users. The good thing is that go on the one hand brought quite revolutionary goroutine, on the one hand, relying on the super factory google, so there are still a lot of people to use it. A good language is not about how shit it is in some places, but whether it has a bright spot that others do not have.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.