idiom for clearing up resources?
Serge Aleynikov
serge@REDACTED
Mon Aug 25 14:40:20 CEST 2003
You can use catch facility with pattern patching:
case catch file:make_dir("c:/tmp/foo") of
ok -> do_some_stuff();
{'EXIT', Reason} -> file:del_dir("c:/tmp/foo")
end
or
Result = catch file:make_dir("c:/tmp/foo"),
file:del_dir("c:/tmp/foo"),
case Result of
ok -> do_some_stuff();
{'EXIT', Reason} -> io:format('Error making dir: ~p~n", [Reason])
end;
WILLIAMS Dominic wrote:
> Hello,
>
> Is there an idiom for initializing and clearing up resources
> (e.g. files) needed by an Erlang function, in such a way that the
> clear-up code gets called even in the face of an error?
>
> e.g.
>
> myfunc() ->
> ok = file:make_dir("c:/tmp/foo"),
> % do some stuff
> ok = file:del_dir("c:/tmp/foo").
>
> I want the last line to be called even if "do some stuff" crashes.
> Basically, coming from a C++ background, this is the code I would
> have put in something's destructor, and I am wondering what Erlangers
> do...
>
> Cheers,
>
> Dominic.
>
More information about the erlang-questions
mailing list