try ... after semantics

Bjorn Gustavsson <>
Wed Aug 17 09:19:50 CEST 2005


Thomas Lindgren <> writes:

> What is the semantics of try ... after? In particular,
> what variable bindings are available in the after
> body?

Only variables introduced BEFORE the try construction.

> 
> Q1: My guess is, one cannot rely on any bindings made
> inside the try being available? (which is why the
> example below, from the erlang reference manual, 6.19,
> crashes if the file:open/2 does not return {ok,...})
> 

Correct. The compiler will not allow you to use any unsafe
variables.

> 
> termize_file(Name) ->
>    try file:open(Name, [read,binary]) of
>       {ok, F} ->
>          %% original code
>          try ... after ... end;
>       Else ->
>          Else
>    end.
> 
> Q2: So, is there a better way of expressing the "safe"
> termize_file using try?

Not as far as I know.

/Björn
-- 
Björn Gustavsson, Erlang/OTP, Ericsson AB



More information about the erlang-questions mailing list