1 {-# OPTIONS_GHC -fno-warn-deprecations #-}
4 module Control.Exception,
9 import Prelude hiding (catch)
11 import Control.Exception
13 catchIO :: IO a -> (IOException -> IO a) -> IO a
16 handleIO :: (IOException -> IO a) -> IO a -> IO a
17 handleIO = flip catchIO
19 tryIO :: IO a -> IO (Either IOException a)
22 -- | A monad that can catch exceptions. A minimal definition
23 -- requires a definition of 'gcatch'.
25 -- Implementations on top of 'IO' should implement 'gblock' and 'gunblock' to
26 -- eventually call the primitives 'Control.Exception.block' and
27 -- 'Control.Exception.unblock' respectively. These are used for
28 -- implementations that support asynchronous exceptions. The default
29 -- implementations of 'gbracket' and 'gfinally' use 'gblock' and 'gunblock'
30 -- thus rarely require overriding.
32 class Monad m => ExceptionMonad m where
34 -- | Generalised version of 'Control.Exception.catch', allowing an arbitrary
35 -- exception handling monad instead of just 'IO'.
36 gcatch :: Exception e => m a -> (e -> m a) -> m a
38 -- | Generalised version of 'Control.Exception.mask_', allowing an arbitrary
39 -- exception handling monad instead of just 'IO'.
40 gmask :: ((m a -> m a) -> m b) -> m b
42 -- | Generalised version of 'Control.Exception.bracket', allowing an arbitrary
43 -- exception handling monad instead of just 'IO'.
44 gbracket :: m a -> (a -> m b) -> (a -> m c) -> m c
46 -- | Generalised version of 'Control.Exception.finally', allowing an arbitrary
47 -- exception handling monad instead of just 'IO'.
48 gfinally :: m a -> m b -> m a
50 -- | DEPRECATED, here for backwards compatibilty. Instances can
51 -- define either 'gmask', or both 'block' and 'unblock'.
53 -- | DEPRECATED, here for backwards compatibilty Instances can
54 -- define either 'gmask', or both 'block' and 'unblock'.
55 gunblock :: m a -> m a
56 -- XXX we're keeping these two methods for the time being because we
57 -- have to interact with Haskeline's MonadException class which
58 -- still has block/unblock; see GhciMonad.hs.
60 gmask f = gblock (f gunblock)
61 gblock f = gmask (\_ -> f)
62 gunblock f = f -- XXX wrong; better override this if you need it
64 gbracket before after thing =
65 gmask $ \restore -> do
67 r <- restore (thing a) `gonException` after a
72 gmask $ \restore -> do
73 r <- restore a `gonException` sequel
77 #if __GLASGOW_HASKELL__ < 613
78 instance ExceptionMonad IO where
80 gmask f = block $ f unblock
84 instance ExceptionMonad IO where
86 gmask f = mask (\x -> f x)
91 gtry :: (ExceptionMonad m, Exception e) => m a -> m (Either e a)
92 gtry act = gcatch (act >>= \a -> return (Right a))
93 (\e -> return (Left e))
95 -- | Generalised version of 'Control.Exception.handle', allowing an arbitrary
96 -- exception handling monad instead of just 'IO'.
97 ghandle :: (ExceptionMonad m, Exception e) => (e -> m a) -> m a -> m a
100 -- | Always executes the first argument. If this throws an exception the
101 -- second argument is executed and the exception is raised again.
102 gonException :: (ExceptionMonad m) => m a -> m b -> m a
103 gonException ioA cleanup = ioA `gcatch` \e ->
105 throw (e :: SomeException)