Skip to content

Instantly share code, notes, and snippets.

@leikind
Last active August 29, 2015 14:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leikind/1dc02b99826051e835e6 to your computer and use it in GitHub Desktop.
Save leikind/1dc02b99826051e835e6 to your computer and use it in GitHub Desktop.
Errors and processes in erlang

DECEMBER 30, 2011

Previous posts about http://learnyousomeerlang.com: Dog FSM, Cat FSM.

Chapter Errors And Processes is closer to the beginning of the book and deals with core Erlang concepts: error propagation and error trapping.

First off, here is what I wrote to better understand the section that starts with words "the results of uncaught throws, errors and exits in neighboring processes":

leikind/1541608

Here how the demo works:

1> c(simple_exit_demo).
./simple_exit_demo.erl:12: Warning: this expression will fail with a 'badarith' exception
{ok,simple_exit_demo}
2> simple_exit_demo:simple_exit_demo().
<0.38.0>
Caught a system message: {'EXIT',<0.39.0>,normal}

Caught a system message: {'EXIT',<0.40.0>,exiting_for_a_reason}

Caught a system message: {'EXIT',<0.41.0>,normal}

Caught a system message: {'EXIT',<0.42.0>,
                             {badarith,
                                 [{simple_exit_demo,
                                      '-start_processes/0-fun-3-',0}]}}

3>
=ERROR REPORT==== 30-Dec-2011::22:42:58 ===
Error in process <0.42.0> with exit value: {badarith,[{simple_exit_demo,'-start_processes/0-fun-3-',0}]}

Caught a system message: {'EXIT',<0.43.0>,
                             {raising_error,
                                 [{simple_exit_demo,
                                      '-start_processes/0-fun-4-',0}]}}

3>
=ERROR REPORT==== 30-Dec-2011::22:42:59 ===
Error in process <0.43.0> with exit value: {raising_error,[{simple_exit_demo,'-start_processes/0-fun-4-',0}]}


=ERROR REPORT==== 30-Dec-2011::22:43:00 ===
Error in process <0.44.0> with exit value: {{nocatch,throwing_error},[{simple_exit_demo,'-start_processes/0-fun-5-',0}]}

Caught a system message: {'EXIT',<0.44.0>,
                             {{nocatch,throwing_error},
                              [{simple_exit_demo,'-start_processes/0-fun-5-',
                                   0}]}}

Show's over
3>

The second chunk of code I've written helps understand how the kill reason is different from other kill reasons:

leikind/1541632

The system process traps an exit and lives on, while exit(Pid, kill) kills it:

1> c(kill).
{ok,kill}
2> kill:kill_demo().
I, <0.38.0>, live on
I, <0.38.0>, live on
I, <0.38.0>, live on
Sending exit(Pid, please_die) <0.38.0> to
<0.38.0> has caught a system message: {'EXIT',<0.31.0>,please_die}

I, <0.38.0>, live on
I, <0.38.0>, live on
I, <0.38.0>, live on
Sending exit(Pid, kill) <0.38.0> to
** exception exit: killed
3>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment