garrison

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

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 exit with exit reason Reason.
erlang — OTP 29.0.2 (erts 17.0.2)

while exit/2 sends an exit signal:

Sends an exit signal with exit reason Reason to the process or port identified by Pid.
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

jswanner

I suppose this is the documentation you were looking for:

The functions erlang:exit/1 and erlang:exit/2 are named similarly but provide very different functionalities. The erlang:exit/1 function should be used when the intent is to stop the current process while erlang:exit/2 should be used when the intent is to send an exit signal to another process. Note also that erlang:exit/1 raises an exception that can be caught while erlang:exit/2 does not cause any exception to be raised.

derek-zhou

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

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

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!

Where Next?

Popular in Questions Top

electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New

Other popular topics Top

baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 44265 214
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 42716 114
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New

We're in Beta

About us Mission Statement