1 % -----------------------------------------------------------------------------
2 % $Id: PrelException.lhs,v 1.20 2000/06/09 13:49:35 simonmar Exp $
4 % (c) The GRAP/AQUA Project, Glasgow University, 1998
7 Exceptions and exception-handling functions.
10 {-# OPTIONS -fno-implicit-prelude #-}
13 module PrelException where
20 import PrelST ( STret(..) )
26 %*********************************************************
28 \subsection{Exception datatype and operations}
30 %*********************************************************
34 = IOException IOError -- IO exceptions (from 'ioError')
35 | ArithException ArithException -- Arithmetic exceptions
36 | ArrayException ArrayException -- Array-related exceptions
37 | ErrorCall String -- Calls to 'error'
38 | NoMethodError String -- A non-existent method was invoked
39 | PatternMatchFail String -- A pattern match / guard failure
40 | RecSelError String -- Selecting a non-existent field
41 | RecConError String -- Field missing in record construction
42 | RecUpdError String -- Record doesn't contain updated field
43 | AssertionFailed String -- Assertions
44 | DynException Dynamic -- Dynamic exceptions
45 | AsyncException AsyncException -- Externally generated errors
46 | PutFullMVar -- Put on a full MVar
47 | BlockedOnDeadMVar -- Blocking on a dead MVar
65 = IndexOutOfBounds String -- out-of-range array access
66 | UndefinedElement String -- evaluating an undefined element
68 stackOverflow, heapOverflow :: Exception -- for the RTS
69 stackOverflow = AsyncException StackOverflow
70 heapOverflow = AsyncException HeapOverflow
72 instance Show ArithException where
73 showsPrec _ Overflow = showString "arithmetic overflow"
74 showsPrec _ Underflow = showString "arithmetic underflow"
75 showsPrec _ LossOfPrecision = showString "loss of precision"
76 showsPrec _ DivideByZero = showString "divide by zero"
77 showsPrec _ Denormal = showString "denormal"
79 instance Show AsyncException where
80 showsPrec _ StackOverflow = showString "stack overflow"
81 showsPrec _ HeapOverflow = showString "heap overflow"
82 showsPrec _ ThreadKilled = showString "thread killed"
84 instance Show ArrayException where
85 showsPrec _ (IndexOutOfBounds s)
86 = showString "array index out of range"
87 . (if not (null s) then showString ": " . showString s
89 showsPrec _ (UndefinedElement s)
90 = showString "undefined array element"
91 . (if not (null s) then showString ": " . showString s
94 instance Show Exception where
95 showsPrec _ (IOException err) = shows err
96 showsPrec _ (ArithException err) = shows err
97 showsPrec _ (ArrayException err) = shows err
98 showsPrec _ (ErrorCall err) = showString err
99 showsPrec _ (NoMethodError err) = showString err
100 showsPrec _ (PatternMatchFail err) = showString err
101 showsPrec _ (RecSelError err) = showString err
102 showsPrec _ (RecConError err) = showString err
103 showsPrec _ (RecUpdError err) = showString err
104 showsPrec _ (AssertionFailed err) = showString err
105 showsPrec _ (AsyncException e) = shows e
106 showsPrec _ (DynException _err) = showString "unknown exception"
107 showsPrec _ (PutFullMVar) = showString "putMVar: full MVar"
108 showsPrec _ (BlockedOnDeadMVar) = showString "thread blocked indefinitely"
109 showsPrec _ (NonTermination) = showString "<<loop>>"
112 %*********************************************************
114 \subsection{Primitive catch and throw}
116 %*********************************************************
119 throw :: Exception -> a
124 throw exception = raise# exception
128 catchException used to handle the passing around of the state to the
129 action and the handler. This turned out to be a bad idea - it meant
130 that we had to wrap both arguments in thunks so they could be entered
131 as normal (remember IO returns an unboxed pair...).
135 catch# :: IO a -> (b -> IO a) -> IO a
137 (well almost; the compiler doesn't know about the IO newtype so we
138 have to work around that in the definition of catchException below).
141 catchException :: IO a -> (Exception -> IO a) -> IO a
143 catchException m k = ST (\s -> unST m s `primCatch'` \ err -> unST (k err) s)
145 catchException (IO m) k = IO $ \s -> catch# m (\ex -> unIO (k ex)) s
148 catch :: IO a -> (IOError -> IO a) -> IO a
149 catch m k = catchException m handler
150 where handler (IOException err) = k err
151 handler other = throw other
153 catchNonIO :: IO a -> (Exception -> IO a) -> IO a
154 catchNonIO m k = catchException m handler
155 where handler (IOException err) = ioError err
156 handler other = k other
160 %*********************************************************
162 \subsection{Try and bracket}
164 %*********************************************************
166 The construct @try comp@ exposes errors which occur within a
167 computation, and which are not fully handled. It always succeeds.
170 try :: IO a -> IO (Either IOError a)
171 try f = catch (do r <- f
175 bracket :: IO a -> (a -> IO b) -> (a -> IO c) -> IO c
176 bracket before after m = do
184 -- variant of the above where middle computation doesn't want x
185 bracket_ :: IO a -> (a -> IO b) -> IO c -> IO c
186 bracket_ before after m = do
196 %*********************************************************
200 %*********************************************************
202 Why is this stuff here? To avoid recursive module dependencies of
206 ioError :: IOError -> IO a
207 ioError err = IO $ \s -> throw (IOException err) s
208 -- (ioError e) isn't an exception; we only throw
209 -- the exception when applied to a world
212 %*********************************************************
214 \subsection{Controlling asynchronous exception delivery}
216 %*********************************************************
220 blockAsyncExceptions :: IO a -> IO a
221 blockAsyncExceptions (IO io) = IO $ blockAsyncExceptions# io
223 unblockAsyncExceptions :: IO a -> IO a
224 unblockAsyncExceptions (IO io) = IO $ unblockAsyncExceptions# io
226 -- Not implemented yet in Hugs.
227 blockAsyncExceptions :: IO a -> IO a
228 blockAsyncExceptions (IO io) = IO io
230 unblockAsyncExceptions :: IO a -> IO a
231 unblockAsyncExceptions (IO io) = IO io