[erlang-questions] Erlang style question - input handling

Nohl Attila Rajmund <>
Thu Sep 28 10:30:20 CEST 2006


On Thu, 28 Sep 2006, Richard A. O'Keefe wrote:
[...]
> As a matter of fact, there IS something very similar, and someone who
> knows C++ and Java should be familiar with it:
>
>    f(X, Y) ->
> 	try
> 	    ... collect some data ...
> 	    check1(data1),
> 	    check2(data2),
> 	    check3(data3),
> 	    ... do something with the collected data
> 	catch
> 	    check1 -> ERROR_CODE1;
> 	    check2 -> ERROR_CODE2;
> 	    check3 -> ERROR_CODE3
> 	end.
>
>    check1(Data) ->
> 	... if all is well, answer true ...
> 	... if something is wrong, throw(check1) ...
>
>    similar for check2, check3.

That looks good.

> By the way, I have very serious doubts about the interface of this
> function.  Why should the caller have to work so hard to figure out
> whether they got a good result or a bad one?  The natural thing to do
> in C++ or Java is *NOT* to return an error code but to throw an
> error term, so the expected structure for f/2 would be
>
> 	f(X, Y) ->
> 	     ... collect some data ...
> 	     check1(data1),
> 	     check2(data2),
> 	     check3(data3),
> 	     ... do something with the collected data ...
>
> and the way the caller finds out about a problem is by using
> a try/catch.

Unfortunately I can't change the caller interface. Crashing would be
also a bad solution, because it's a kind of user input validation and
the user prefers a nice error dialog over an Erlang crash dump.

 				Bye,NAR
-- 
"Beware of bugs in the above code; I have only proved it correct, not
tried it."



More information about the erlang-questions mailing list