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(..),
22 throw, throwIO, ioError )
31 %*********************************************************
33 \subsection{Primitive catch}
35 %*********************************************************
37 catchException used to handle the passing around of the state to the
38 action and the handler. This turned out to be a bad idea - it meant
39 that we had to wrap both arguments in thunks so they could be entered
40 as normal (remember IO returns an unboxed pair...).
44 catch# :: IO a -> (b -> IO a) -> IO a
46 (well almost; the compiler doesn't know about the IO newtype so we
47 have to work around that in the definition of catchException below).
50 catchException :: IO a -> (Exception -> IO a) -> IO a
52 catchException m k = ST (\s -> unST m s `primCatch'` \ err -> unST (k err) s)
54 catchException (IO m) k = IO $ \s -> catch# m (\ex -> unIO (k ex)) s
57 catch :: IO a -> (IOError -> IO a) -> IO a
58 catch m k = catchException m handler
59 where handler (IOException err) = k err
60 handler other = throw other
64 %*********************************************************
66 \subsection{Controlling asynchronous exception delivery}
68 %*********************************************************
71 -- | Applying 'block' to a computation will
72 -- execute that computation with asynchronous exceptions
73 -- /blocked/. That is, any thread which
74 -- attempts to raise an exception in the current thread will be
75 -- blocked until asynchronous exceptions are enabled again. There\'s
76 -- no need to worry about re-enabling asynchronous exceptions; that is
77 -- done automatically on exiting the scope of
81 -- | To re-enable asynchronous exceptions inside the scope of
82 -- 'block', 'unblock' can be
83 -- used. It scopes in exactly the same way, so on exit from
84 -- 'unblock' asynchronous exception delivery will
86 unblock :: IO a -> IO a
89 block (IO io) = IO $ blockAsyncExceptions# io
90 unblock (IO io) = IO $ unblockAsyncExceptions# io
92 unblock :: IO a -> IO a
93 unblock (IO io) = IO io