The model - what if our process crashes?

We originally used spawn/1 to spawn our process, but there is a better way to do it overall. The trouble with just using spawn is that if something happens to that for any reason, there's not really a good way to track it! Let's test this out by replacing our behavior in the receive to instead just raise an exception:

process = fn ->  receive do    _ -> raise "Oh no!"  endend

Now in IEx, if we spawn this and attempt to do anything with it by sending it a message, we'll get an error message about a raised exception inside of a process:

iex(2)> pid = spawn(process)#PID<0.151.0>iex(3)> send(pid, :test):test18:08:16.958 [error] Process #PID<0.151.0> raised an exception** (RuntimeError) Oh no! (stdlib) erl_eval.erl:668: ...

Get Phoenix Web Development now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.