Unexpected try/catch behaviour

Laszlo Lovei <>
Wed Feb 24 10:56:56 CET 2010

On Feb 24, 12:11 am, "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?

I always find the former more appropriate. I usually only want to
catch specific exceptions from specific expressions, and consider
other exceptions as errors. In this situation, "try case E of H end
catch ... end" can incidentally catch errors, while a simple "try E of
H catch ... end" does exactly what I want. I admit that in most "try"
expressions I don't need the "of H" part, but when I need it, "try ...
of" is always more appropriate than "try case ... of".

Another point: if you want to catch exceptions from H, you can write
"try case E of H end catch ... end". However, it is much harder to
write code that doesn't catch exceptions from H: in "case try E
catch ... end of H end" you must extend H with clauses that do
something in case of a caught exception. (The "ets:slot" example was
easy, because the exception was treated in the same way as the end of
the table, but normally exceptions signal exceptional situations,
which should be treated differently.)


More information about the erlang-questions mailing list