From ca42310d56e946d3e266ae89b525f1d297ce15c0 Mon Sep 17 00:00:00 2001 From: simonmar Date: Fri, 11 Feb 2005 11:36:23 +0000 Subject: [PATCH 1/1] [project @ 2005-02-11 11:36:23 by simonmar] Add bracketOnError --- Control/Exception.hs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/Control/Exception.hs b/Control/Exception.hs index cb4ba3e..5362610 100644 --- a/Control/Exception.hs +++ b/Control/Exception.hs @@ -103,6 +103,7 @@ module Control.Exception ( bracket, -- :: IO a -> (a -> IO b) -> (a -> IO c) -> IO () bracket_, -- :: IO a -> IO b -> IO c -> IO () + bracketOnError, finally, -- :: IO a -> IO b -> IO a @@ -390,6 +391,21 @@ a `finally` sequel = bracket_ :: IO a -> IO b -> IO c -> IO c bracket_ before after thing = bracket before (const after) (const thing) +-- | Like bracket, but only performs the final action if there was an +-- exception raised by the in-between computation. +bracketOnError + :: IO a -- ^ computation to run first (\"acquire resource\") + -> (a -> IO b) -- ^ computation to run last (\"release resource\") + -> (a -> IO c) -- ^ computation to run in-between + -> IO c -- returns the value from the in-between computation +bracketOnError before after thing = + block (do + a <- before + catch + (unblock (thing a)) + (\e -> do { after a; throw e }) + ) + -- ----------------------------------------------------------------------------- -- Asynchronous exceptions -- 1.7.10.4