Exception.hs 2.79 KB
Newer Older
1 2 3

module Exception
    (
4 5 6 7 8 9 10
    module Control.Exception,
    module Exception
    )
    where

import Prelude hiding (catch)

11
import Control.Exception
12 13 14

catchIO :: IO a -> (IOException -> IO a) -> IO a
catchIO = catch
15

16 17
handleIO :: (IOException -> IO a) -> IO a -> IO a
handleIO = flip catchIO
18

19 20
tryIO :: IO a -> IO (Either IOException a)
tryIO = try
21

22 23 24
-- | A monad that can catch exceptions.  A minimal definition
-- requires a definition of 'gcatch'.
--
25 26 27 28 29 30 31
-- Implementations on top of 'IO' should implement 'gblock' and 'gunblock' to
-- eventually call the primitives 'Control.Exception.block' and
-- 'Control.Exception.unblock' respectively.  These are used for
-- implementations that support asynchronous exceptions.  The default
-- implementations of 'gbracket' and 'gfinally' use 'gblock' and 'gunblock'
-- thus rarely require overriding.
--
32
class Monad m => ExceptionMonad m where
33

34 35 36 37
  -- | Generalised version of 'Control.Exception.catch', allowing an arbitrary
  -- exception handling monad instead of just 'IO'.
  gcatch :: Exception e => m a -> (e -> m a) -> m a

38 39 40 41 42 43 44 45
  -- | Generalised version of 'Control.Exception.block', allowing an arbitrary
  -- exception handling monad instead of just 'IO'.
  gblock :: m a -> m a

  -- | Generalised version of 'Control.Exception.unblock', allowing an
  -- arbitrary exception handling monad instead of just 'IO'.
  gunblock :: m a -> m a

46 47 48 49 50 51 52 53
  -- | Generalised version of 'Control.Exception.bracket', allowing an arbitrary
  -- exception handling monad instead of just 'IO'.
  gbracket :: m a -> (a -> m b) -> (a -> m c) -> m c

  -- | Generalised version of 'Control.Exception.finally', allowing an arbitrary
  -- exception handling monad instead of just 'IO'.
  gfinally :: m a -> m b -> m a

54 55 56 57 58 59 60
  gblock = id
  gunblock = id

  gbracket before after thing =
    gblock (do
      a <- before
      r <- gunblock (thing a) `gonException` after a
61
      _ <- after a
62
      return r)
63

64 65 66
  a `gfinally` sequel =
    gblock (do
      r <- gunblock a `gonException` sequel
67
      _ <- sequel
68
      return r)
69 70 71

instance ExceptionMonad IO where
  gcatch    = catch
72 73
  gblock    = block
  gunblock  = unblock
74 75 76 77 78 79 80 81 82 83 84 85 86 87

gtry :: (ExceptionMonad m, Exception e) => m a -> m (Either e a)
gtry act = gcatch (act >>= \a -> return (Right a))
                  (\e -> return (Left e))

-- | Generalised version of 'Control.Exception.handle', allowing an arbitrary
-- exception handling monad instead of just 'IO'.
ghandle :: (ExceptionMonad m, Exception e) => (e -> m a) -> m a -> m a
ghandle = flip gcatch

-- | Always executes the first argument.  If this throws an exception the
-- second argument is executed and the exception is raised again.
gonException :: (ExceptionMonad m) => m a -> m b -> m a
gonException ioA cleanup = ioA `gcatch` \e ->
88
                             do _ <- cleanup
89
                                throw (e :: SomeException)
90