Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Go Error Handling Techniques [closed]

Tags:

go

I'm just getting started with Go. My code is starting to have a lot of this:

   if err != nil {       //handle err    } 

or this

  if err := rows.Scan(&some_column); err != nil {       //handle err   } 

Are there some good idioms/strategies/best-practices for checking and handling errors in Go?

EDIT to clarify: I'm not bellyaching or suggesting that the Go team come up with something better. I'm asking if I'm doing it right or have I missed some technique that the community came up with. Thanks all.

like image 940
gmoore Avatar asked Jun 06 '13 13:06

gmoore


People also ask

How do you handle errors in Go?

Go's built-in errors don't contain stack traces, nor do they support conventional try / catch methods to handle them. Instead, errors in Go are just values returned by functions, and they can be treated in much the same way as any other datatype - leading to a surprisingly lightweight and simple design.

Does Golang have exception handling?

Defer, panic and recover. Go does not have exceptions like many other programming languages, including Java and Javascript but has a comparable mechanism know as ,,Defer, panic and recover".

How do you wrap errors in Go?

In Go 1.13, though, Go added support for wrapping and unwrapping errors as part of the standard library by adding the errors. Unwrap function and the %w verb for the fmt. Errorf function. In this section, you'll update your program to use the %w verb to wrap errors with more information, and you'll then use errors.

Does Golang have try catch?

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

Your code is idiomatic and in my opinion it is the best practice available. Some would disagree for sure, but I would argue that this is the style seen all over the standard libraries in Golang. In other words, Go authors write error handling in this way.

like image 50
zzzz Avatar answered Sep 21 '22 13:09

zzzz


Six months after this question was asked Rob Pike wrote a blog post titled Errors are Values.

In there he argues that you don't need to program in the way presented by the OP, and mentions several places in the standard library where they use a different pattern.

Of course a common statement involving an error value is to test whether it is nil, but there are countless other things one can do with an error value, and application of some of those other things can make your program better, eliminating much of the boilerplate that arises if every error is checked with a rote if statement.

...

Use the language to simplify your error handling.

But remember: Whatever you do, always check your errors!

It's a good read.

like image 22
Michael Deardeuff Avatar answered Sep 19 '22 13:09

Michael Deardeuff