pm100

pm100

Difference between try / rescue and try / throw

Whats the difference between the two? They seem to do identical things but one is doced as ‘only use very rarely’

First Post!

dogweather

dogweather

I got a little confused by those docs too, and made a mental note to go back and see if I could figure out the difference.

But about the “only use very rarely”, I feel confident shedding some light on that.

Maybe someone will come along and correct me, but I believe that the Elixir ecosystem is fairly opinionated (in a good way) about not catching exceptions/errors. 95% of the time we should just ignore them and let them crash the app. Then, we look at the crashes, which should be only due to programming errors, and fix our bugs.

Said differently, The exceptions are generally things outside of our control that we can’t really program for. And: this is how we should design our own code: to use {:ok, :error} for business logic errors, and reaching for raising exceptions for unsalvageable inconsistencies.

Most Liked

josevalim

josevalim

Creator of Elixir

Semantically:

  • A throw is meant to be caught by you (typically within the same module that throws), can be any term

  • An error is when something goes wrong (typically not rescued), it is an exception in Elixir

  • An exit is when a process is crashing (typically not caught), can be any term

throws are handled by catchs, errors are handled by rescues. If I had a magic wand, exit would not be part of the language, especially because it is often mixed with the separate exit signal, but it exists in Erlang, so we have to support it, hence the catch kind, reason -> notation.

PRs to improve the docs are always welcome.

mudasobwa

mudasobwa

Creator of Cure

I would treat try/catch as a control flow (goto-like,) while try/rescue as handling an exceptional situation.

al2o3cr

al2o3cr

rescue is a shorthand, mostly. catch can do its job, but there’s more boilerplate and some of the shapes aren’t as nice:

try do
  1 + :foo
rescue
  e ->
    IO.puts "OH NO #{inspect(e)}"
end

(prints OH NO %ArithmeticError{message: "bad argument in arithmetic expression"})
versus

try do
  1 + :foo
catch
  :error, e ->
    IO.puts "OH NO #{inspect(e)}"
end

which prints OH NO :badarith

Using catch in this way also means that Erlang errors are not transformed into Elixir exceptions - note the different inspect output for the two cases.

Last Post!

gregvaughn

gregvaughn

This is oversimplified, but throw/catch comes from Erlang. The payload can be any Erlang term (usually a tuple) and it relies on historical implicit conventions of how to use it. Elixir uses raise/rescue to encode/decode an Exception struct as the payload as an explicit convention.

Where Next?

Popular in Questions Top

joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
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
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

Other popular topics Top

New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
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
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New

We're in Beta

About us Mission Statement