The quirks of exit signals and exception handling you always forget
Trapping exits means the exit signals are converted to messages before being sent to the process. Trapping exits is done on a per process basis. Execute process_flag(trap_exit, true).
in the process you want to be trapping exits.
Exit Type | Initiated by | Not trapping exits | Trapping exits |
Normal | exit(Pid, normal) |
Nothing | Receives {'EXIT', Pid, normal} |
Normal | exit(self(), normal) |
Terminates normally | Receives {'EXIT', Pid, normal} |
Abnormal | exit(Pid, Reason) |
Terminates abnormally | Receives {'EXIT', Pid, Reason} |
Kill | exit(Pid, kill) |
Terminates abnormally | Terminates abnormally |
Purpose | Expression to generate | try catch handling pattern |
trap_exit message |
Stop execution because of runtime error | exit/2 |
error:Reason |
{'EXIT', Pid, Reason} |
Stop execution for any reason | exit/1 |
exit:Reason |
{'EXIT', Pid, Reason} |
non-local return | throw/1 |
throw:Reason |
{'EXIT', Pid, {{nocatch, Reason}, MFA}} |
badmatch
badarg
badarith
undef
function_clause
if_clause
case_clause
Name | Type | Purpose | Function to Create / Example Calls |
Function to Destroy / Example Calls |
Link | Symmetrical | Propagate exit signal from a process to all linked processes | link/1 , spawn_link/1,2,3,4 link(Pid). |
unlink/1 unlink(Pid) |
Monitor | Asymmetrical | Sends a message to the monitoring process when the monitored process dies | monitor/2 , spawn_monitor/1,3 monitor(Pid) |
demonitor/1,2 demonitor(Pid) |