Erlang - Error Handling

Three BIFs (in erlang module):
- error(Reason): runtime error, e.g. divide by zero, bad match, trying to call a function which doesn’t exist etc
- throw(Exception): throw exception that let caller catch it
- exit(Reason): implicitly(no code to run) or explicitly exit
- exit(Pid, Reason): send exit signal Reason to Pid

- exit(normal): when process run out of code or exit explicitly with a ‘normal’ reason. normal signal will propagate to its linked processes, but won’t kill/exit them
- exit(kill): will kill current process, signal kill will propagate to linked processes, link set processes can trap_exit it. If linked processes don’t trap_exit, them will be killed and propagate killed signal to their linked processes
- exit(Pid, kill): exit explicitly with a ‘kill’ reason, will always kill the Pid (even Pid has trap_exit), a killed signal will propagate to Pid’s linked processes
- exit(Reason): exit explicitly with a not ‘normal’ reason

- Process who executes one of the above functions will exit (or let Pid exit).
- Exit signals will be propagated to all linked processes(by link or spawn_link BIF). If it is abnormal exit signal (not exit(normal) signal), linked processes will be killed as well.
- Each linked process, if it is killed, not exit(normal), will send the exit signal on it’s own Pid, with the same Reason or with ‘normal’ Reason(if it is exit normally), to it’s linked processes instead.

- process_flag(trap_exit, true):
When trap_exit is set to true, exit signals(include normal signal but not include kill signal) arriving to the current process are converted to {‘EXIT’, FromPid, Reason} messages, which can be received as ordinary messages. That means even if it receives an abnormal exit signal, it won’t be killed. Default trap_exit is false.

- if calling link (LinkedPid) fails, the process will receive ‘noproc’ exit signal from LinkedPid.

While link processes is symmetric (two directions), there is another option to use as asymmetric way (one directions): erlang:monitor(process, Item) -> MonitorRef