There are two common ways to handle errors:
- throw exception
- e.g.
panicin Golang
- e.g.
- return error code
- return 0 or -1 in Unix system calls
Here is one point of view to decide which method to go with.
The rule is simple: if your function is in any way likely to fail, it should return an error code. When I’m calling some other package, if it is well written I don’t have to worry about panics, except for, well, truly exceptional conditions, things I shouldn’t be expected to handle.
To know more. Here is a short discussion by Ross Cox, where he suggested these two articles: