1 % -----------------------------------------------------------------------------
2 % $Id: PrelException.lhs,v 1.7 1999/05/18 14:59:16 simonpj 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
18 import PrelST ( STret(..) )
24 -----------------------------------------------------------------------------
25 Exception datatype and operations.
29 = IOException IOError -- IO exceptions (from 'ioError')
30 | ArithException ArithException -- Arithmetic exceptions
31 | ErrorCall String -- Calls to 'error'
32 | NoMethodError String -- A non-existent method was invoked
33 | PatternMatchFail String -- A pattern match failed
34 | NonExhaustiveGuards String -- A guard match failed
35 | RecSelError String -- Selecting a non-existent field
36 | RecConError String -- Field missing in record construction
37 | RecUpdError String -- Record doesn't contain updated field
38 | AssertionFailed String -- Assertions
39 | DynException Dynamic -- Dynamic exceptions
40 | AsyncException AsyncException -- Externally generated errors
56 stackOverflow, heapOverflow :: Exception -- for the RTS
57 stackOverflow = AsyncException StackOverflow
58 heapOverflow = AsyncException HeapOverflow
60 instance Show ArithException where
61 showsPrec _ Overflow = showString "arithmetic overflow"
62 showsPrec _ Underflow = showString "arithmetic underflow"
63 showsPrec _ LossOfPrecision = showString "loss of precision"
64 showsPrec _ DivideByZero = showString "divide by zero"
65 showsPrec _ Denormal = showString "denormal"
67 instance Show AsyncException where
68 showsPrec _ StackOverflow = showString "stack overflow"
69 showsPrec _ HeapOverflow = showString "heap overflow"
70 showsPrec _ ThreadKilled = showString "thread killed"
72 instance Show Exception where
73 showsPrec _ (IOException err) = shows err
74 showsPrec _ (ArithException err) = shows err
75 showsPrec _ (ErrorCall err) = showString err
76 showsPrec _ (NoMethodError err) = showString err
77 showsPrec _ (PatternMatchFail err) = showString err
78 showsPrec _ (NonExhaustiveGuards err) = showString err
79 showsPrec _ (RecSelError err) = showString err
80 showsPrec _ (RecConError err) = showString err
81 showsPrec _ (RecUpdError err) = showString err
82 showsPrec _ (AssertionFailed err) = showString err
83 showsPrec _ (AsyncException e) = shows e
84 showsPrec _ (DynException _err) = showString "unknown exception"
88 throw :: Exception -> a
93 throw exception = raise# exception
97 catch handles the passing around of the state in the IO monad; if we
98 don't actually apply (and hence run) an IO computation, we don't get
99 any exceptions! Hence a large mantrap to watch out for is
101 catch# (m :: IO ()) (handler :: NDSet Exception -> IO ())
103 since the computation 'm' won't actually be performed in the context
104 of the 'catch#'. In fact, don't use catch# at all.
107 catchException :: IO a -> (Exception -> IO a) -> IO a
109 catchException m k = ST (\s -> unST m s `primCatch'` \ err -> unST (k err) s)
111 catchException m k = IO $ \s -> case catch# (liftIO m s) (\exs -> liftIO (k exs) s)
112 of STret s1 r -> (# s1, r #)
115 catch :: IO a -> (IOError -> IO a) -> IO a
116 catch m k = catchException m handler
117 where handler (IOException err) = k err
118 handler other = throw other
120 catchNonIO :: IO a -> (Exception -> IO a) -> IO a
121 catchNonIO m k = catchException m handler
122 where handler (IOException err) = ioError err
123 handler other = k other
127 Why is this stuff here? To avoid recursive module dependencies of
131 ioError :: IOError -> IO a
132 ioError err = throw (IOException err)