Programs fail for many reasons. A file may not exist. Memory allocation may fail. Input may be malformed. A network connection may close unexpectedly.
| Section | Title |
|---|---|
| 1 | Error Sets |
| 2 | Error Unions |
| 3 | Returning Errors |
| 4 | try |
| 5 | catch |
| 6 | defer and errdefer |
| 7 | Designing Error Boundaries |
| 8 | Exercises |
Error SetsPrograms fail for many reasons. A file may not exist. Memory allocation may fail. Input may be malformed. A network connection may close unexpectedly.
Error UnionsAn error union combines a normal value with an error set.
Returning ErrorsA function returns an error the same way it returns a normal value: with return.
`try`try is a shortcut for a common operation:
`catch`catch handles an error union.
`defer` and `errdefer`A program that opens a file must close it. A program that allocates memory must free it. A program that locks a mutex must unlock it.
Designing Error BoundariesMost functions should not decide what an error means.
Exercises1. Write a function parseUpper that accepts a byte and returns the uppercase letter value. Return error.InvalidUppercase if the byte is not between 'A' and 'Z'.