[erlang-questions] Unexpected try/catch behaviour

Per Melin per.melin@REDACTED
Wed Feb 24 01:05:17 CET 2010


On Wed, Feb 24, 2010 at 12:11 AM, Richard O'Keefe <ok@REDACTED> wrote:
> I had forgotten about try...of...catch...end.
> When is it appropriate to use try E of H catch ... end
> instead of try case E of H end catch ... end?
>
> I think the _intent_ is clear, that in try E ... end
> it is *only* E that is protected.
>
> It does make sense, but it would be really nice to see
> a couple of examples where try .. of .. catch .. end was
> the right thing to do.

How about when you need to catch a specific event?

try ets:slot(Table, SlotNumber) of
    '$end_of_table' ->
        ok;
    Items ->
        do_stuff(Items)
catch
    error:badarg ->
        % We get badarg when the slot number was higher than the total
        % number of slots. This can happen if someone deleted items from
        % the table while we were traversing it. No worries.
        ok
end.

I don't want to accidentally catch a badarg in do_stuff/1 here.


More information about the erlang-questions mailing list