1 % ------------------------------------------------------------------------------
2 % $Id: PrelException.lhs,v 1.24 2000/09/14 14:24:02 simonmar Exp $
4 % (c) The University of Glasgow, 1998-2000
7 Exceptions and exception-handling functions.
10 {-# OPTIONS -fno-implicit-prelude #-}
14 ( module PrelException,
15 Exception(..), AsyncException(..),
16 IOException(..), ArithException(..), ArrayException(..),
27 %*********************************************************
29 \subsection{Primitive catch}
31 %*********************************************************
33 catchException used to handle the passing around of the state to the
34 action and the handler. This turned out to be a bad idea - it meant
35 that we had to wrap both arguments in thunks so they could be entered
36 as normal (remember IO returns an unboxed pair...).
40 catch# :: IO a -> (b -> IO a) -> IO a
42 (well almost; the compiler doesn't know about the IO newtype so we
43 have to work around that in the definition of catchException below).
46 catchException :: IO a -> (Exception -> IO a) -> IO a
48 catchException m k = ST (\s -> unST m s `primCatch'` \ err -> unST (k err) s)
50 catchException (IO m) k = IO $ \s -> catch# m (\ex -> unIO (k ex)) s
53 catch :: IO a -> (Exception -> IO a) -> IO a
54 catch m k = catchException m handler
55 where handler err@(IOException _) = k err
56 handler err@(UserError _) = k err
57 handler other = throw other
61 %*********************************************************
63 \subsection{Try and bracket}
65 %*********************************************************
67 The construct @try comp@ exposes errors which occur within a
68 computation, and which are not fully handled. It always succeeds.
70 These are the IO-only try/bracket. For the full exception try/bracket
71 see hslibs/lang/Exception.lhs.
74 try :: IO a -> IO (Either Exception a)
75 try f = catch (do r <- f
79 bracket :: IO a -> (a -> IO b) -> (a -> IO c) -> IO c
80 bracket before after m = do
88 -- variant of the above where middle computation doesn't want x
89 bracket_ :: IO a -> (a -> IO b) -> IO c -> IO c
90 bracket_ before after m = do
100 %*********************************************************
102 \subsection{Controlling asynchronous exception delivery}
104 %*********************************************************
108 block :: IO a -> IO a
109 block (IO io) = IO $ blockAsyncExceptions# io
111 unblock :: IO a -> IO a
112 unblock (IO io) = IO $ unblockAsyncExceptions# io
114 -- Not implemented yet in Hugs.
115 block :: IO a -> IO a
116 block (IO io) = IO io
118 unblock :: IO a -> IO a
119 unblock (IO io) = IO io