Unexpected try/catch behaviour

Laszlo Lovei lovei.laszlo@REDACTED
Thu Feb 25 17:39:37 CET 2010

On Feb 25, 2:57 am, "Richard O'Keefe" <o...@REDACTED> wrote:
> The tricky thing here is that foo(Data) is
> *outside* the scope of the "catch"
> *inside* the scsope of the "after"
> if I haven't blown all my fuses entirely.
> Useful, this is.  Easy to take into acount when reading, it isn't.
> I think I'd want some comments, being a Bear of very little Brain.
>      Handle = resource:open("foo.txt"),
>      try                                % catch? after?
>          resource:read(Handle)          % yes    yes
>      of {ok, Data} -> foo(Data)         % no     yes
>       ; Error      -> Error
>      catch
>          error:Reason -> {error,Reason}
>      after
>          resource:close(Handle)          % no     no
>      end
> Perhaps nested tries and cases might actually be easier to read.

Funny, if you indent the code like that, I find it straightforward to
read: "try to evaluate read(), return values should be matched against
the of clauses, exceptions against the catch clauses, and after that,
evaluate close()". Why should exceptions from "of" clauses be caught
by "catch" clauses in this structure? They are not nested constructs,
but clauses of the same nesting level in "try". Catching exceptions
from "of" clauses would be analogous to evaluating "case" clauses
sequentially, matching the return value of a clause against the next


More information about the erlang-questions mailing list