This is the case:
% print/1: Prints out the integers between 1 and N
print(0) -> io:format("~w~n", [0]);
print(N) when is_integer(N) ->
io:format("~w~n", [N]),
print(N - 1).
If the user inputs a non-integer, this happens:
11> effects:print('alfalfa').
** exception error: no function clause matching effects:print(alfalfa)
Is about phylosophy: Should I correct my program this way, in order to 'catch all' kinds of input?
% print/1: Prints out the integers between 1 and N
print(0) -> io:format("~w~n", [0]);
print(N) when is_integer(N) ->
io:format("~w~n", [N]),
print(N - 1).
% Last Line added:
print(_Other) -> false.
I'm new in erlang. Is there some convention for dealing with this?
Thanks!
In Erlang you mostly would not catch such bad API usages. If no pattern matches the invocation, an exception of class exit with a rather verbose message would be thrown ({function_clause, CallStack}
).
Almost every standard library method throws. At the moment I fail to think of counterexamples.
Btw: You mostly would return {error, Msg}
, not false, if there was some sort of error (mostly not usage error). In good cases ok
or {ok, Datum}
would be returned.
This might be useful http://mazenharake.wordpress.com/2009/09/14/let-it-crash-the-right-way/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With