Documentation ¶
Overview ¶
errors demonstrates error handling in Go
- Some functions always succeeds so they don't need to return errors.
- Some functions always returns single failure, so a bool can be used.
- An "error" type of argument is required if a function can return multiple errors.
- The other return values are undefined when an error is returned. However, some functions may need to return partial results along with the error. Say a Read() returning partial data with IO errors. Such case should be documented well.
Go took the design decision to go with this error type instead of exceptions:
- Exceptions returns incomprehensible stacktrace, full of information about the structure of the program, but lacking intelligable context about what went wrong.
- By contrast, Go programs use ordinary error handling with return values and if statements. The demands more attention to be paid to error handling. This is the primary reason.
- Go also has panic() and recover() which is a kind of exception handling, but that is used to report truly unexpected exceptions that indicates a bug.
The error type is an interface:
- A nil value is default and indicates no error.
- it has the method "Error() string" to get the error message.
- Package "errors" contains several helpers
- fmt.Errorf is handy to produce error type that uses Sprintf internally
Error handling techniques:
- Propagate the error a. Return as it is b. Add context to the error and form the chain
- Retry for sometime
- Exit with error code (main workflow)
- Ignore the error continue with limited functionality
- Ignore the error and proceed
In general, get into the habit of handling errors returned by functions.
- If you deliberately ignore some errors, document it.
- Handle the error first before performing success action
- If return statement is used for error, then write the success action in the outer block. Do not use else block and minimize in indent.
- So a function tends to handle multiple errors on top and return. Finally the sustance of the function.
Click to show internal directories.
Click to hide internal directories.