Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Panic recover in Go v.s. try catch in other languages

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.

like image 273
Motti Avatar asked Aug 05 '10 09:08

Motti


People also ask

Should you panic in Golang?

Don't use panic for normal error handling. Use error and multiple return values. See https://golang.org/doc/effective_go.html#errors.

What is panic and recover in Golang?

In Go, we use defer, panic and recover statements to handle errors. We use defer to delay the execution of functions that might cause an error. The panic statement terminates the program immediately and recover is used to recover the message during panic.

What is a panic in Golang?

What is panic() in Golang? Samia Ishaque. The panic() function in Go Language is similar to exceptions raised at runtime when an error is encountered. panic() is either raised by the program itself when an unexpected error occurs or the programmer throws the exception on purpose for handling particular errors.

Is there a try catch in go?

Because of these reasons, the Go language does not support the conventional try/catch/finally error handling approach. It has strict error handling which is totally a different approach. Here, we have demonstrated a very basic example of handling errors with the help of panic.


2 Answers

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.

like image 200
Jesse Avatar answered Sep 17 '22 10:09

Jesse


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.

like image 36
cthom06 Avatar answered Sep 21 '22 10:09

cthom06