11

Error handling best practice

 4 years ago
source link: https://yourbasic.org/golang/errors-explained/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Error handling best practice

yourbasic.org/golang
errors.png

Go has two different error-handling mechanisms:

  • most functions return errors;
  • only a truly unrecoverable condition, such as an out-of-range index, produces a run-time exception, known as a panic.

Go’s multivalued return makes it easy to return a detailed error message alongside the normal return value. By convention, such messages have type error, a simple built-in interface:

type error interface {
    Error() string
}

Error handling example

The os.Open function returns a non-nil error value when it fails to open a file.

func Open(name string) (file *File, err error)

The following code uses os.Open to open a file. If an error occurs it calls log.Fatal to print the error message and stop.

f, err := os.Open("filename.ext")
if err != nil {
    log.Fatal(err)
}
// do something with the open *File f

Custom errors

To create a simple string-only error you can use errors.New:

err := errors.New("Houston, we have a problem")

The error interface requires only an Error method, but specific error implementations often have additional methods, allowing callers to inspect the details of the error.

Learn more

See 3 simple ways to create an error for more examples.

Panic

Panics are similar to C++ and Java exceptions, but are only intended for run-time errors, such as following a nil pointer or attempting to index an array out of bounds.

Learn more

panic-thumb.jpg
See Recover from a panic for a tutorial on how to recover from and test panics.

Share:             


Recommend

  • 155

    Error Handling 17 May 2020

  • 92
    • blog.merovius.de 7 years ago
    • Cache

    What even is error handling?

    What even is error handling?January 21, 2018tl;dr: Error handling shouldn’t be about how to best propagate an error value, but how to make it destroy it (or make it irrelevant). To encourage myself to d...

  • 54

    Browsers now have native support for doing asyncronous calls via async/await. This is nice. It is essentially syntax support for promises. How did we end up with this? Well, when adding new features…

  • 48

    In this post, you'll learn how to use exception handling in PHP. As of PHP 5, we can use try catch blocks for error handling—this is a better way to handle exceptions and control the flow of your application. In this arti...

  • 56
    • www.tuicool.com 6 years ago
    • Cache

    C# Futures: Deferred Error Handling

    When writing robust software, there is often a need to perform a series of retriable operations. In order to make the system robust, each operation in the series can be coded so it is independent of the status of the prev...

  • 50

    Errors are values In his post “Errors are values” , Rob Pike, one of the original authors of Go, attends the common perception that one must repetitiv...

  • 16

    React Error Handling and Logging Best PracticesHow to handle errors and properly log them using the right tools

  • 9

    <?xml encoding="utf-8" ??>Introduction This guide explains error handling in Go and the best practices for handling API errors in Go. You should have a working knowledge of Go and understand how...

  • 12

    Quick Microsoft .NET 6 Overview Microsoft .NET 6 is a cross-platform framework that merges the .NET Core, .NET Framew...

  • 4

    Introduction One of the challenges of working with any graphics API is that under the hood they tend to be very asynchronous in nature. The work you send to the GPU isn’t completed the moment you call draw or dispatch,...

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK