idiom for clearing up resources?

Francesco Cesarini francesco@REDACTED
Mon Aug 25 14:10:06 CEST 2003

If you use OTP behaviours, you could trap exits and clean up in the 
terminate call back function. The function is called every time you stop 
your behaviour, or when ever it terminates abnormally and you are 
trapping exits. The cleaning would have to be generic, however, as it 
can be tricky to figure out exactly what you were doing before the crash.

You can read about the OTP behaviours at


WILLIAMS Dominic wrote:

>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?
>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

More information about the erlang-questions mailing list