2011-11-07 38 views
4

这种情况:erlang phylosophy:我应该让用户处理错误的输入吗?

% 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). 

如果用户输入一个非整数,出现这种情况:

11> effects:print('alfalfa'). 
** exception error: no function clause matching effects:print(alfalfa) 

是关于哲学史:我应该纠正我的程序这样,为了“追赶所有'种类的输入?

% 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. 

我是新的erlang。有处理这个问题的一些约定吗?

谢谢!

回答

7

在Erlang中,你大多不会遇到如此糟糕的API用法。如果没有模式匹配调用,则会引发exception of class exit带有相当详细的消息({function_clause, CallStack})。几乎每个标准库方法都会抛出。目前我没有想到反例。

顺便说一句:如果出现某种错误(主要是不使用错误),你大多会返回{error, Msg},而不是假。在好的情况下,ok{ok, Datum}将被退回。

+1

非常感谢! –