[<<][erlang][>>][..]
Mon Jan 16 19:25:04 CET 2017

exit vs throw?

rvirding on http://stackoverflow.com/a/13654007
        

There are 3 classes which can be caught with a try ... catch: throw,
error and exit.

    throw is generated using throw/1 and is intended to be used for
    non-local returns and does not generate an error unless it is not
    caught (when you get a nocatch error).

    error is generated when the system detects an error. You can
    explicitly generate an error using error/1. The system also
    includes a stacktrace in the generated error value, for example
    {badarg,[...]}.

    exit is generated using exit/1 and is intended to signal that this
    process is to die.

The difference between error/1 and exit/1 is not that great, it more
about intention which the stacktrace generated by errors enhances.

The difference between them is actually more noticeable when doing
catch ...: when throw/1 is used then the catch just returns the thrown
value, as is expected from a non-local return; when an error/1 is used
then the catch returns {'EXIT',Reason} where Reason contains the
stacktrace; while from exit/1 catch also returns {'EXIT',Reason} but
Reason only contains the actual exit reason. try ... catch looks like
it equates them, but they are/were very different.






[Reply][About]
[<<][erlang][>>][..]