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 -----------------------------------------------------------------------------
18 ( module GHC.Exception,
19 Exception(..), AsyncException(..),
20 IOException(..), ArithException(..), ArrayException(..),
21 throw, throwIO, ioError )
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
48 catchException (IO m) k = IO $ \s -> catch# m (\ex -> unIO (k ex)) s
50 catch :: IO a -> (IOError -> IO a) -> IO a
51 catch m k = catchException m handler
52 where handler (IOException err) = k err
53 handler other = throw other
57 %*********************************************************
59 \subsection{Controlling asynchronous exception delivery}
61 %*********************************************************
64 -- | Applying 'block' to a computation will
65 -- execute that computation with asynchronous exceptions
66 -- /blocked/. That is, any thread which
67 -- attempts to raise an exception in the current thread will be
68 -- blocked until asynchronous exceptions are enabled again. There\'s
69 -- no need to worry about re-enabling asynchronous exceptions; that is
70 -- done automatically on exiting the scope of
74 -- | To re-enable asynchronous exceptions inside the scope of
75 -- 'block', 'unblock' can be
76 -- used. It scopes in exactly the same way, so on exit from
77 -- 'unblock' asynchronous exception delivery will
79 unblock :: IO a -> IO a
81 block (IO io) = IO $ blockAsyncExceptions# io
82 unblock (IO io) = IO $ unblockAsyncExceptions# io