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’
Most Liked
josevalim
Semantically:
-
A
throwis meant to be caught by you (typically within the same module that throws), can be any term -
An
erroris when something goes wrong (typically not rescued), it is an exception in Elixir -
An
exitis 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
I would treat try/catch as a control flow (goto-like,) while try/rescue as handling an exceptional situation.
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.
LostKobrakai
You‘d use throw (+catch) there. It‘s the usecase it exists for in elixir. Though it‘s used sparingly and if you can replace things with normal control flow/returning that‘s usually preferred.
This is mostly considered to be a separate tool to error handling / rescue.
pm100
The documentation is really not very good.
Syntax
- what is the syntax for ‘catch’, it is not explained anywhere. Docs have
catch
x -> "Got #{x}"
and (you show)
catch
:error, e ->
IO.puts "OH NO #{inspect(e)}"
Note that there is one thing after catch in the first one 2 in the second, what does that mean?
-
what does that ‘->’ mean, it looks like a match, is it?
-
can I have multiple catches to catch different things (like in other languages), if not how do I say ’ i want to catch x, but not y’
-
whats the syntax of rescue, is it different from catch
-
what does this mean
rescue
e in RuntimeError -> e
it suggests that RuntimeError is a collection of some kind. The docs for RuntimeError suggest its a single thing
- I can have catch without try! (hidden away at the end under the description of Exit), how about rescue, how about after
Dynamics
- does a raise propagate to a caller? All the examples just show one code block. Does it flow up the stack like exceptions in , say, c++? Or is it just propagated to the immediate caller.
- how about throw, several times I have seen people say ‘its like a fancy goto’. This strongly suggests that it only works inside one function.
- throw/1 docs says “A non-local return from a function”. Which makes it sound like its just a quick out for a function, and does not flow up the stack.
- what happens if nobody catches a throw
- what happens if I have a try/catch and somebody does a raise?
- what happens if I have a try / rescue and somebody does a throw?
- what happens if nobody rescues a raise
Actually I found the answers to a lot of these here Elixir - try/catch vs try/rescue? - Stack Overflow
This reveals all sorts of stuff that not in the elixir docs anywhere
Nobody has said what the difference between these two are. I don’t mean what are their use cases but, functionally what do they do different that leads to them having different use cases. There must be a difference







