garrison
Why can't I catch Process.exit/2?
Alright, fair warning: I may be missing something obvious here.
I can use try/catch to catch an exit sent with exit/1:
try
exit(:timeout)
catch
:exit, :timeout -> :ok
end
But if I catch an exit sent with Process.exit/2, it doesn’t work (it exits):
try
Process.exit(self(), :timeout)
:timer.sleep(1000)
catch
:exit, :timeout -> :ok
end
What’s really getting me here is I can’t find any mention of this in the docs. I managed to find one random comment which mentions this fact completely off-hand, and that’s it.
Am I missing something?
Marked As Solved
cevado
elixir function Kernel.exit/1 delegates to erlang exit/1 while Process.exit/2 delegates to erlang exit/2.
if you read the erlang documentation:
exit/1 raises an exception:
Raises an exception of class
exitwith exit reasonReason.
erlang — OTP 29.0.2 (erts 17.0.2)
while exit/2 sends an exit signal:
Sends an exit signal with exit reason
Reasonto the process or port identified byPid.
erlang — OTP 29.0.2 (erts 17.0.2)
that’s why a catch can handle an exit/1 call but not an exit/2.
You can get more details on erlang doc for processes:
Also Liked
jswanner
I suppose this is the documentation you were looking for:
The functions
erlang:exit/1anderlang:exit/2are named similarly but provide very different functionalities. Theerlang:exit/1function should be used when the intent is to stop the current process whileerlang:exit/2should be used when the intent is to send an exit signal to another process. Note also thaterlang:exit/1raises an exception that can be caught whileerlang:exit/2does not cause any exception to be raised.
derek-zhou
You are correct. Ask yourself this question: Why try … catch should catch asynchronous event? Do you expect this to catch also?
Process.exit(self(), :timeout)
try
:timer.sleep(1000)
catch
:exit, :timeout -> :ok
end
You can; you just need to do Process.flag(:trap_exit, true) and have a handle_info clause to handle the {:EXIT, pid, reason} message that you would receive.
jswanner
I think you want to look at Process.flag/2 and the meaning of :trap_exit. It still won’t do what you want, as you’ll need to handle it by receiving the message in the process mailbox
Last Post!
garrison
And just to be clear, there are some things which work this way. For example, what happens here?
Process.send_after(self(), :foo, 1)
receive do
:foo -> :ok
after
1 -> :error
end
It’s a race!
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex









