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

New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
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
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
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
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New

Other popular topics Top

rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
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
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
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