[erlang-questions] Unexpected try/catch behaviour

Garrett Smith <>
Wed Feb 24 01:28:33 CET 2010


On Tue, Feb 23, 2010 at 5:36 PM, Bernard Duggan <> wrote:
> Richard O'Keefe wrote:
>> When is it appropriate to use try E of H catch ... end
>> instead of try case E of H end catch ... end?
> When you want to use tail recursion.
> The recursive call to foo() here:
>
> foo(X) ->
>    try
>        case f(X) of
>            A -> foo(A);
>            _ -> ok
>       end
>    catch
>       _ -> doom()
>    end
>
> Is /not/ tail recursive on account of the catch block.

Another problem with that block is that you don't know whether the
exception originated from f(X) or foo(A).

The beauty of try...of is that you isolate the code your trying.

Garrett


More information about the erlang-questions mailing list