Exception handling: panic/exception v.s. return error

Posted on April 20, 2016 Updated on April 20, 2016

There are two common ways to handle errors:

  • throw exception
    • e.g. panic in Golang
  • 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: