1 % ------------------------------------------------------------------------------
2 % $Id: Exception.lhs,v 1.2 2001/07/03 14:13:32 simonmar Exp $
4 % (c) The University of Glasgow, 1998-2000
7 Exceptions and exception-handling functions.
10 {-# OPTIONS -fno-implicit-prelude #-}
14 ( module GHC.Exception,
15 Exception(..), AsyncException(..),
16 IOException(..), ArithException(..), ArrayException(..),
28 %*********************************************************
30 \subsection{Primitive catch}
32 %*********************************************************
34 catchException used to handle the passing around of the state to the
35 action and the handler. This turned out to be a bad idea - it meant
36 that we had to wrap both arguments in thunks so they could be entered
37 as normal (remember IO returns an unboxed pair...).
41 catch# :: IO a -> (b -> IO a) -> IO a
43 (well almost; the compiler doesn't know about the IO newtype so we
44 have to work around that in the definition of catchException below).
47 catchException :: IO a -> (Exception -> IO a) -> IO a
49 catchException m k = ST (\s -> unST m s `primCatch'` \ err -> unST (k err) s)
51 catchException (IO m) k = IO $ \s -> catch# m (\ex -> unIO (k ex)) s
54 catch :: IO a -> (Exception -> IO a) -> IO a
55 catch m k = catchException m handler
56 where handler err@(IOException _) = k err
57 handler err@(UserError _) = k err
58 handler other = throw other
62 %*********************************************************
64 \subsection{Try and bracket}
66 %*********************************************************
68 The construct @try comp@ exposes errors which occur within a
69 computation, and which are not fully handled. It always succeeds.
71 These are the IO-only try/bracket. For the full exception try/bracket
72 see hslibs/lang/Exception.lhs.
75 try :: IO a -> IO (Either Exception a)
76 try f = catch (do r <- f
80 bracket :: IO a -> (a -> IO b) -> (a -> IO c) -> IO c
81 bracket before after m = do
89 -- variant of the above where middle computation doesn't want x
90 bracket_ :: IO a -> (a -> IO b) -> IO c -> IO c
91 bracket_ before after m = do
101 %*********************************************************
103 \subsection{Controlling asynchronous exception delivery}
105 %*********************************************************
109 block :: IO a -> IO a
110 block (IO io) = IO $ blockAsyncExceptions# io
112 unblock :: IO a -> IO a
113 unblock (IO io) = IO $ unblockAsyncExceptions# io
115 -- Not implemented yet in Hugs.
116 block :: IO a -> IO a
117 block (IO io) = IO io
119 unblock :: IO a -> IO a
120 unblock (IO io) = IO io