Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exception handling in Google Go language

I am wondering... I have read about Go some time ago and I tried to program something in it. I seems quite interesting. But I have reached handling "exceptions" in this language. I have read about their approach and it seems reasonable. I would like to know what are the advantages of the standard exceptional approach over the Go's style? What are the pros and cons?

Edit To be straight: I do not want to make any holy war about exceptions. I just wonder if this style of handling errors has any advantages? What are actual advantages of this style over standard exceptions? Is it worth wondering at all?

like image 261
Archie Avatar asked Aug 25 '11 22:08

Archie


People also ask

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".

Does Go language have exceptions?

Why does Go not have exceptions? We believe that coupling exceptions to a control structure, as in the try-catch-finally idiom, results in convoluted code. It also tends to encourage programmers to label too many ordinary errors, such as failing to open a file, as exceptional.

How do you check errors in Golang?

Errors can be returned as nil , and in fact, it's the default, or “zero”, value of on error in Go. This is important since checking if err != nil is the idiomatic way to determine if an error was encountered (replacing the try / catch statements you may be familiar with in other programming languages).


1 Answers

panic/recover is moral equivalent of try/catch exceptions. There is superficial difference (syntax) and a subtle, but important, difference of intended use.

The best explanations of problems with exceptions in general is "Cleaner, more elegant, wrong" and that's a good overview of pros/cons of exceptions vs. returning error codes.

Go designers decided that error handling by returning error codes from functions is the idiomatic Go way and the language supports multiple return values to make it syntactically easy. While panic/recover is provided, the difference is not of functionality but intended use.

Other languages exposing exceptions promote their use and in practice they are used frequently (sometimes even misused).

Go discourages the use of panic/recover. You can do it but you're only supposed to do it in very limited scenarios.

If you look at Go's own standard library, most uses of panic are for signaling fatal errors, indicating either an internal error (i.e. bug) in the library code or calling the library with wrong data (e.g. passing non-json data to json decoding functions).

But as the article you linked to points out: "The convention in the Go libraries is that even when a package uses panic internally, its external API still presents explicit error return values."

This is different from languages like C#, Java, Python or C++, where a lot of standard library code can throw exceptions to signal errors. Those languages want you to use exceptions. Go discourages the use of panic/recover.

To summarize:

  • idiomatic Go style is to use error codes to tell the caller about errors
  • use panic/recover only in rare cases:
    • to "crash" your program when encountering internal inconsistency indicating bugs in your code. It's basically a debugging aid.
    • if it dramatically simplifies error handling in your code (but if the code is to be used by others, never expose such panics to callers)

In practice the important thing is to use language's idiomatic style. In Go that's returning error codes and avoiding panic/recover. In C# that's using exceptions to signal some of the errors.

like image 98
Krzysztof Kowalczyk Avatar answered Sep 28 '22 06:09

Krzysztof Kowalczyk