[erlang-questions] Old school exceptions vs. new age one

Maxim Treskin zerthurd@REDACTED
Mon Apr 28 14:27:27 CEST 2008


Hello

According Pragmatic Programming Erlang there is two syntax of exceptions:
Old-style:
<pre>
case (catch foo(...)) of
    {'EXIT', Why} ->
        ...
    Val ->
       ...
end
</pre>

and new-style:
<pre>
try foo(...) of
    Val -> ...
catch
    exit: Why ->
         ...
end
</pre>

If I use new-style syntax, I have not detailed exception cause
explanation, like this:
<pre>
3> try lists:sort([1,2,3]) catch exit:Why -> io:format("~p~n", [Why]) end.
[1,2,3]
4> try lists:sort(abc) catch exit:Why -> io:format("~p~n", [Why]) end.
** exception error: function_clause
</pre>

But if I use old-style syntax, I have this:
<pre>
5> case (catch lists:sort(abc)) of {'EXIT', Why} -> io:format("~p~n",
[Why]) end.
{function_clause,[{lists,sort,[abc]},
                  {erl_eval,do_apply,5},
                  {erl_eval,expr,5},
                  {erl_eval,expr,5},
                  {shell,exprs,6},
                  {shell,eval_exprs,6},
                  {shell,eval_loop,3}]}
ok
</pre>

Can you say me, why is new-style better than old?

Thank you

-- 
Maxim Treskin



More information about the erlang-questions mailing list