Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e42133e

Browse files
crabonaturejosevalim
authored andcommitted
Process exiting messages (elixir-lang#1045)
1 parent dc678fa commit e42133e

File tree

2 files changed

+15
-10
lines changed

2 files changed

+15
-10
lines changed

getting-started/processes.markdown

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -112,18 +112,23 @@ iex> spawn fn -> raise "oops" end
112112
113113
[error] Process #PID<0.58.00> raised an exception
114114
** (RuntimeError) oops
115-
:erlang.apply/2
115+
(stdlib) erl_eval.erl:668: :erl_eval.do_apply/6
116116
```
117117

118118
It merely logged an error but the parent process is still running. That's because processes are isolated. If we want the failure in one process to propagate to another one, we should link them. This can be done with `spawn_link/1`:
119119

120120
```iex
121-
iex> spawn_link fn -> raise "oops" end
121+
iex> self()
122122
#PID<0.41.0>
123+
iex> spawn_link fn -> raise "oops" end
123124
124-
** (EXIT from #PID<0.41.0>) an exception was raised:
125+
** (EXIT from #PID<0.41.0>) evaluator process exited with reason: an exception was raised:
125126
** (RuntimeError) oops
126-
:erlang.apply/2
127+
(stdlib) erl_eval.erl:668: :erl_eval.do_apply/6
128+
129+
[error] Process #PID<0.289.0> raised an exception
130+
** (RuntimeError) oops
131+
(stdlib) erl_eval.erl:668: :erl_eval.do_apply/6
127132
```
128133

129134
Because processes are linked, we now see a message saying the parent process, which is the shell process, has received an EXIT signal from another process causing the shell to terminate. IEx detects this situation and starts a new shell session.
@@ -146,9 +151,10 @@ iex(1)> Task.start fn -> raise "oops" end
146151
147152
15:22:33.046 [error] Task #PID<0.55.0> started from #PID<0.53.0> terminating
148153
** (RuntimeError) oops
149-
(elixir) lib/task/supervised.ex:74: Task.Supervised.do_apply/2
150-
(stdlib) proc_lib.erl:239: :proc_lib.init_p_do_apply/3
151-
Function: #Function<20.90072148/0 in :erl_eval.expr/5>
154+
(stdlib) erl_eval.erl:668: :erl_eval.do_apply/6
155+
(elixir) lib/task/supervised.ex:85: Task.Supervised.do_apply/2
156+
(stdlib) proc_lib.erl:247: :proc_lib.init_p_do_apply/3
157+
Function: #Function<20.99386804/0 in :erl_eval.expr/5>
152158
Args: []
153159
```
154160

getting-started/try-catch-and-rescue.markdown

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ For the cases where you do expect a file to exist (and the lack of that file is
9696
```iex
9797
iex> File.read! "unknown"
9898
** (File.Error) could not read file unknown: no such file or directory
99-
(elixir) lib/file.ex:305: File.read!/1
99+
(elixir) lib/file.ex:272: File.read!/1
100100
```
101101

102102
Many functions in the standard library follow the pattern of having a counterpart that raises an exception instead of returning tuples to match against. The convention is to create a function (`foo`) which returns `{:ok, result}` or `{:error, reason}` tuples and another function (`foo!`, same name but with a trailing `!`) that takes the same arguments as `foo` but which raises an exception if there's an error. `foo!` should return the result (not wrapped in a tuple) if everything goes fine. The [`File` module](https://hexdocs.pm/elixir/File.html) is a good example of this convention.
@@ -134,8 +134,7 @@ All Elixir code runs inside processes that communicate with each other. When a p
134134

135135
```iex
136136
iex> spawn_link fn -> exit(1) end
137-
#PID<0.56.0>
138-
** (EXIT from #PID<0.56.0>) 1
137+
** (EXIT from #PID<0.56.0>) evaluator process exited with reason: 1
139138
```
140139

141140
In the example above, the linked process died by sending an `exit` signal with a value of 1. The Elixir shell automatically handles those messages and prints them to the terminal.

0 commit comments

Comments
 (0)