2 {-# OPTIONS -fno-implicit-prelude #-}
3 -----------------------------------------------------------------------------
5 -- Module : GHC.Exception
6 -- Copyright : (c) The University of Glasgow, 1998-2002
7 -- License : see libraries/base/LICENSE
9 -- Maintainer : cvs-ghc@haskell.org
10 -- Stability : internal
11 -- Portability : non-portable (GHC extensions)
13 -- Exceptions and exception-handling functions.
15 -----------------------------------------------------------------------------
19 ( module GHC.Exception,
20 Exception(..), AsyncException(..),
21 IOException(..), ArithException(..), ArrayException(..),
33 %*********************************************************
35 \subsection{Primitive catch}
37 %*********************************************************
39 catchException used to handle the passing around of the state to the
40 action and the handler. This turned out to be a bad idea - it meant
41 that we had to wrap both arguments in thunks so they could be entered
42 as normal (remember IO returns an unboxed pair...).
46 catch# :: IO a -> (b -> IO a) -> IO a
48 (well almost; the compiler doesn't know about the IO newtype so we
49 have to work around that in the definition of catchException below).
52 catchException :: IO a -> (Exception -> IO a) -> IO a
54 catchException m k = ST (\s -> unST m s `primCatch'` \ err -> unST (k err) s)
56 catchException (IO m) k = IO $ \s -> catch# m (\ex -> unIO (k ex)) s
59 catch :: IO a -> (Exception -> IO a) -> IO a
60 catch m k = catchException m handler
61 where handler err@(IOException _) = k err
62 handler other = throw other
66 %*********************************************************
68 \subsection{Try and bracket}
70 %*********************************************************
72 The construct @try comp@ exposes errors which occur within a
73 computation, and which are not fully handled. It always succeeds.
75 These are the IO-only try/bracket. For the full exception try/bracket
76 see hslibs/lang/Exception.lhs.
79 try :: IO a -> IO (Either Exception a)
80 try f = catch (do r <- f
84 bracket :: IO a -> (a -> IO b) -> (a -> IO c) -> IO c
85 bracket before after m = do
93 -- variant of the above where middle computation doesn't want x
94 bracket_ :: IO a -> (a -> IO b) -> IO c -> IO c
95 bracket_ before after m = do
105 %*********************************************************
107 \subsection{Controlling asynchronous exception delivery}
109 %*********************************************************
112 -- | Applying 'block' to a computation will
113 -- execute that computation with asynchronous exceptions
114 -- /blocked/. That is, any thread which
115 -- attempts to raise an exception in the current thread will be
116 -- blocked until asynchronous exceptions are enabled again. There\'s
117 -- no need to worry about re-enabling asynchronous exceptions; that is
118 -- done automatically on exiting the scope of
120 block :: IO a -> IO a
122 -- | To re-enable asynchronous exceptions inside the scope of
123 -- 'block', 'unblock' can be
124 -- used. It scopes in exactly the same way, so on exit from
125 -- 'unblock' asynchronous exception delivery will
126 -- be disabled again.
127 unblock :: IO a -> IO a
130 block (IO io) = IO $ blockAsyncExceptions# io
131 unblock (IO io) = IO $ unblockAsyncExceptions# io
133 unblock :: IO a -> IO a
134 unblock (IO io) = IO io