2 ms·
>Every function that can fail should return an error code Every function that can fail, that can take invalid arguments, or can be called at invalid times. Whi
by wruza 7y ago
>Every function that can fail should return an error code
Every function that can fail, that can take invalid arguments, or can be called at invalid times. Which widens the set to basically every function. Old APIs had a convention to always return a status directly and results in out-params, except for pure mathematical operations (in which errors were mostly ignored, e.g. in a computer, a+b may fail as much as fopen(), but who cares).
Observing this fact, one can free themselves from:
if ((status=operation(args, &vague_error_descriptor))
return status;
and handle the specific error event where it seems reasonable in the way that seems reasonable.
Unwinding call stack by hand doesn’t automagically handle errors. At main() or at the other high-level call site you still have no idea what to do with an unexpected error except logging/presenting it and exiting (or continuing in hope invariants were not broken).
>one can use automated tools to find all the places where error codes are being ignored
The same is true for exceptions - every milestone like db.open(), file.save() and process.spawn() should be try-catched. And in both cases you don’t know which errors “codes” are still unhandled.
- loopz 7y agoYou are describing panic() which may happen due to programmer error, but not from a+b or any arbitrary event. It follows a short spec and stdlib, and may even be recoverable. Your if-example is old style and not allowed in golang. Your milestones escalates any small deviation into potential crash-handling territory (good example).