Version française
Home     About     Download     Resources     Contact us    
Browse thread
unwind-protect?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Colin Walters <walters@c...>
Subject: unwind-protect?
Hello,

Is there an equivalent of Lisp's `unwind-protect' or Java's
"try...finally"?  I wanted to write a function which changed to
another directory temporarily, and I came up with:

let in_directory d f =
  let olddir = Unix.getcwd() in
  (Unix.chdir d;
   f();
   Unix.chdir olddir)

But this won't work if f throws an exception.  Any suggestions?