error_handling/

directory
v0.0.0-...-361c87b Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 4, 2017 License: Apache-2.0

README

Error Handling

Error handling is critical for making your programs reliable, trustworthy and respectful to those who depend on them. A proper error value is both specific and informative. It must allow the caller to make an informed decision about the error that has occurred. There are several ways in Go to create error values. This depends on the amount of context that needs to be provided.

Notes

  • Use the default error value for static and simple formatted messages.
  • Create and return error variables to help the caller identify specific errors.
  • Create custom error types when the context of the error is more complex.
  • Error Values in Go aren't special, they are just values like any other, and so you have the entire language at your disposal.

http://blog.golang.org/error-handling-and-go
http://www.goinggo.net/2014/10/error-handling-in-go-part-i.html
http://www.goinggo.net/2014/11/error-handling-in-go-part-ii.html
http://clipperhouse.com/2015/02/07/bugs-are-a-failure-of-prediction/
http://dave.cheney.net/2014/12/24/inspecting-errors
http://dave.cheney.net/2016/04/27/dont-just-check-errors-handle-them-gracefully
Errors are handled in return values

Code Review

Default Error Values (Go Playground)
Error Variables (Go Playground)
Type As Context (Go Playground)
Behavior As Context (Go Playground)
Find The Bug (Go Playground) | The Reason (Go Playground)

Exercises

Exercise 1

Create two error variables, one called ErrInvalidValue and the other called ErrAmountTooLarge. Provide the static message for each variable. Then write a function called checkAmount that accepts a float64 type value and returns an error value. Check the value for zero and if it is, return the ErrInvalidValue. Check the value for greater than $1,000 and if it is, return the ErrAmountTooLarge. Write a main function to call the checkAmount function and check the return error value. Display a proper message to the screen.

Template (Go Playground) | Answer (Go Playground)

Exercise 2

Create a custom error type called appError that contains three fields, err error, message string and code int. Implement the error interface providing your own message using these three fields. Implement a second method named temporary that returns false when the value of the code field is 9. Write a function called checkFlag that accepts a bool value. If the value is false, return a pointer of your custom error type initialized as you like. If the value is true, return a default error. Write a main function to call the checkFlag function and check the error using the temporary interface.

Template (Go Playground) | Answer (Go Playground)


All material is licensed under the Apache License Version 2.0, January 2004.

Directories

Path Synopsis
Sample program to show how the default error type is implemented.
Sample program to show how the default error type is implemented.
Sample program to show how to use error variables to help the caller determine the exact error being returned.
Sample program to show how to use error variables to help the caller determine the exact error being returned.
http://golang.org/src/pkg/encoding/json/decode.go Sample program to show how to implement a custom error type based on the json package in the standard library.
http://golang.org/src/pkg/encoding/json/decode.go Sample program to show how to implement a custom error type based on the json package in the standard library.
Package example5 provides code to show how to implement behavior as context.
Package example5 provides code to show how to implement behavior as context.
Sample program to show see if the class can find the bug.
Sample program to show see if the class can find the bug.
reason
Sample program to show see if the class can find the bug.
Sample program to show see if the class can find the bug.
exercises
exercise1
Create two error variables, one called ErrInvalidValue and the other called ErrAmountTooLarge.
Create two error variables, one called ErrInvalidValue and the other called ErrAmountTooLarge.
exercise2
Create a custom error type called appError that contains three fields, err error, message string and code int.
Create a custom error type called appError that contains three fields, err error, message string and code int.
template1
Create two error variables, one called ErrInvalidValue and the other called ErrAmountTooLarge.
Create two error variables, one called ErrInvalidValue and the other called ErrAmountTooLarge.
template2
Create a custom error type called appError that contains three fields, err error, message string and code int.
Create a custom error type called appError that contains three fields, err error, message string and code int.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL