Forgive me for my ignorance, while perusing a few elixir/phoenix codes, I came across portions where rescue and catch were being used without a corresponding try block. Is this a syntactic sugar which implicitly have a try block?, does the community frown upon using it as I’ve read a few elixir books but never encountered the construct. IMHO it looks cleaner to me. Thank you.
def testing_rescue do
raise "You need to be rescue"
rescue
_ -> "I've been rescued by elixirforum"
end
Those clauses act on the whole function body and basically remove one level of nesting if the whole body should be subject to the recovery: https://hexdocs.pm/elixir/Kernel.html#def/2-rescue-catch-after-else
I wouldn’t say it’s frowned upon, but simply not used often (like try) and people usually prefer other means of error handling.
6 Likes
Thanks for the swift reply, very much appreciated.
Clean code is great
use it!!
1 Like
I like this syntax, but I cant get it work inside anonymous functions.
Is it supposed to work ?
Don’t think so, since it’s part of def
macro
1 Like