0
votes

While Elixir encourages us not to use try/catch blocks there are times when they are needed. In an application I wrote I have the following try/catch which works.

try do 
  message = GenServer.call(via, :get_messages)
  {:ok, message}
catch 
  :exit, _ -> {:error, "Process uuid no longer exists"}
end

In the catch section I changed :exit, - to {:exit, _} thinking that they were the same thing and that resulted in breaking the code.

My question is what is :exit, _

It does not seam to be a tuple, list, etc. Even when I used _ it did not match. Is catch a macro that expects parameters?

1
Relevant: My answer on the 3 types of exceptions in Erlang: stackoverflow.com/a/45973542/320615 - Dogbert

1 Answers

2
votes

Elixir is open source and it has a nearly perfect documentation.

Kernel.SpecialForms.try/1’s subsection on catching throws and exits reveals the whole thing.

Yes, try is a macro that is inlined by compiler and depending on the clause’s signature is transpiled to either :throw, _ or to _, _.