[erlang-questions] When to return {ok, Value} or just the Value

Garrett Smith g@REDACTED
Tue Nov 15 18:34:28 CET 2011


On Tue, Nov 15, 2011 at 11:07 AM, David Mercer <dmercer@REDACTED> wrote:
> Why is your second alternative (viz., “f(X) -> Y | exit(..)”) not the
> default preferred option for all Erlang code?  It seems to express your
> meaning and intent better than a tuple return code.  You can always wrap it
> in a try if you want to catch the error.
>
> Me, I always have trouble deciding between exit(…) and error(…).

I was just looking into this myself and ran into this very helpful paper:

http://bit.ly/rQJvoD

My take on when to use:

- Use exit(Term) to convey exceptions/errors that are part of your
documented API - there's no need to convey the stack information
because the origin and nature of the error is unambiguous

- Use error(Term) to generate errors that aren't documented -- i.e.
errors that aren't anticipated / expected - you want to preserve the
stack information for debugging

I'd be interested in hearing others thoughts here.

Garrett



More information about the erlang-questions mailing list