Version française
Home     About     Download     Resources     Contact us    
Browse thread
exception safety / RAII ?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Michael Benfield <leftfist@m...>
Subject: exception safety / RAII ?
I'm looking at OCaml coming from sort of a C++ background and I'm 
finding it really exciting.

There's one thing that worries me though. C++ programmers have been 
dealing with issues of exception safety for years - it's a complicated 
problem because coding in the presence of exceptions for all intents 
and purposes means your function could end at any point, so how can you 
make sure resources are deallocated? The C++ solution to this problem 
is a technique called Resource Acquisition Is Initialization. C++ 
objects have destructors, which are simply functions that will always 
be called on exit from a scope - including if the exit is caused by an 
exception coming up through your function. You make resource release 
(whether the resource is memory, a socket, whatever) happen in a 
destructor, and then you are set. This is very handy even disregarding 
exceptions.

So I'm just wondering what facilities OCaml has to either implement 
this concept, or other concepts to help with exception safety? The 
OCaml manual says: "Also, finalization can be performed by trapping all 
exceptions, performing the finalization, then raising again the 
exception". This makes me cringe.