I want to catch zero division error but do not know which exact pattern should I write for that
Result = try 5/0 catch {'EXIT',{badarith,_}} -> 0.
It works when I catch all the exceptions via
Result = try 5/0 catch _:_ -> 0.
but the first example gives
** exception error: an error occurred when evaluating an arithmetic expression
So how to properly catch zero division
You may use this code which I get from http://learnyousomeerlang.com/errors-and-exceptions
catcher(X,Y) ->
case catch X/Y of
{'EXIT', {badarith,_}} -> "uh oh";
N -> N
end.
6> c(exceptions).
{ok,exceptions}
7> exceptions:catcher(3,3).
1.0
8> exceptions:catcher(6,3).
2.0
9> exceptions:catcher(6,0).
"uh oh"
OR
catcher(X, Y) ->
try
X/Y
catch
error:badarith -> 0
end.