Background
Both try/rescue
and try/catch
are error handling techniques in Elixir. According the corresponding chapter in the introduction guide.
Errors can be rescued using the
try/rescue
construct
On the other hand,
throw
andcatch
are reserved for situations where it is not possible to retrieve a value unless by usingthrow
andcatch
.
Doubts
I have a brief understanding that rescue
is for errors. While catch
is for any value.
However,
- When should I make use of the error handling mechanisms in Elixir?
- What are the differences between them in detail?
- How should I pick one to use in a specific use case?
- What exactly are 'the situations where it is not possible to retrieve a value unless by using
throw
andcatch
'?
try/catch
all errors. You should use supervisors when error occured to make your app robust or use raise whenever error should occur. – PatNowak