bexley
bexley

Reputation: 69

Elixir: try do catch exit signals

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?

Upvotes: 0

Views: 1004

Answers (1)

Aleksei Matiushkin
Aleksei Matiushkin

Reputation: 121000

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 _, _.

Upvotes: 2

Related Questions