Erlang / Erlang Advanced Interview questions
What is the difference between error, exit, and throw in Erlang?
All three raise an exception that unwinds the call stack until caught, but they carry different intent and default severity.
| error/1 | exit/1 | throw/1 |
| Signals a programming/runtime error; includes a stack trace; typically left uncaught to crash the process. | Signals intentional process termination; propagates to linked processes as an exit signal. | A lightweight, expected non-local return; meant to be caught by the same code that anticipated it. |
try error(bad_input) %% class = error catch error:Reason -> handle(Reason); exit:Reason -> handle_exit(Reason); throw:Reason -> handle_throw(Reason) end.
The rule of thumb: use throw for expected, local control flow inside code you also control the
catch for; use error for genuine bugs you want visible in crash logs with a stack trace; use
exit when you specifically want to terminate a process and have that termination propagate to
anything linked to it.
More Related questions...