views:

214

answers:

3

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.

+2  A: 

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.

Mue
+5  A: 

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 block will run unless catch 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.

cthom06
Thanks, BTW, `defer` is very similar to D's `scope`
Motti
@Motti I haven't really taken a close look @ D, I'll have to check that out.
cthom06
`defer` is relatively unique to Go in the way that there is a keyword, but it just stems from the idea of doing useful things in stack unwinding. I'll freely admit I LOVE the idea that exceptions should not be used as much as they are today.
Matthieu M.
+2  A: 

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.

Jessta