[project @ 1999-05-10 16:52:10 by sof]
[ghc-hetmet.git] / ghc / lib / std / PrelException.lhs
1 % -----------------------------------------------------------------------------
2 % $Id: PrelException.lhs,v 1.6 1999/05/10 16:52:10 sof Exp $
3 %
4 % (c) The GRAP/AQUA Project, Glasgow University, 1998
5 %
6
7 Exceptions and exception-handling functions.
8
9 \begin{code}
10 {-# OPTIONS -fno-implicit-prelude #-}
11
12 #ifndef __HUGS__
13 module PrelException where
14
15 import PrelBase
16 import PrelIOBase
17 import PrelST           ( STret(..) )
18 import PrelDynamic
19 import PrelGHC
20 #endif
21 \end{code}
22
23 -----------------------------------------------------------------------------
24 Exception datatype and operations.
25
26 \begin{code}
27 data Exception
28   = IOException         IOError         -- IO exceptions (from 'ioError')
29   | ArithException      ArithException  -- Arithmetic exceptions
30   | ErrorCall           String          -- Calls to 'error'
31   | NoMethodError       String          -- A non-existent method was invoked
32   | PatternMatchFail    String          -- A pattern match failed
33   | NonExhaustiveGuards String          -- A guard match failed
34   | RecSelError         String          -- Selecting a non-existent field
35   | RecConError         String          -- Field missing in record construction
36   | RecUpdError         String          -- Record doesn't contain updated field
37   | AssertionFailed     String          -- Assertions
38   | DynException        Dynamic         -- Dynamic exceptions
39   | AsyncException      AsyncException  -- Externally generated errors
40
41 data ArithException
42   = Overflow
43   | Underflow
44   | LossOfPrecision
45   | DivideByZero
46   | Denormal
47   deriving (Eq, Ord)
48
49 data AsyncException
50   = StackOverflow
51   | HeapOverflow
52   | ThreadKilled
53   deriving (Eq, Ord)
54
55 stackOverflow, heapOverflow :: Exception -- for the RTS
56 stackOverflow = AsyncException StackOverflow
57 heapOverflow  = AsyncException HeapOverflow
58
59 instance Show ArithException where
60   showsPrec _ Overflow        = showString "arithmetic overflow"
61   showsPrec _ Underflow       = showString "arithmetic underflow"
62   showsPrec _ LossOfPrecision = showString "loss of precision"
63   showsPrec _ DivideByZero    = showString "divide by zero"
64   showsPrec _ Denormal        = showString "denormal"
65
66 instance Show AsyncException where
67   showsPrec _ StackOverflow   = showString "stack overflow"
68   showsPrec _ HeapOverflow    = showString "heap overflow"
69   showsPrec _ ThreadKilled    = showString "thread killed"
70
71 instance Show Exception where
72   showsPrec _ (IOException err)          = shows err
73   showsPrec _ (ArithException err)       = shows err
74   showsPrec _ (ErrorCall err)            = showString err
75   showsPrec _ (NoMethodError err)        = showString err
76   showsPrec _ (PatternMatchFail err)     = showString err
77   showsPrec _ (NonExhaustiveGuards err)  = showString err
78   showsPrec _ (RecSelError err)          = showString err
79   showsPrec _ (RecConError err)          = showString err
80   showsPrec _ (RecUpdError err)          = showString err
81   showsPrec _ (AssertionFailed err)      = showString err
82   showsPrec _ (AsyncException e)         = shows e
83   showsPrec _ (DynException _err)        = showString "unknown exception"
84
85 -- Primitives:
86
87 throw :: Exception -> a
88
89 #ifdef __HUGS__
90 throw = primRaise
91 #else
92 throw exception = raise# exception
93 #endif
94 \end{code}
95
96 catch handles the passing around of the state in the IO monad; if we
97 don't actually apply (and hence run) an IO computation, we don't get
98 any exceptions!  Hence a large mantrap to watch out for is
99
100         catch# (m :: IO ()) (handler :: NDSet Exception -> IO ())
101
102 since the computation 'm' won't actually be performed in the context
103 of the 'catch#'.  In fact, don't use catch# at all.
104
105 \begin{code}
106 catchException :: IO a -> (Exception -> IO a) -> IO a
107 #ifdef __HUGS__
108 catchException m k =  ST (\s -> unST m s `primCatch'` \ err -> unST (k err) s)
109 #else
110 catchException m k =  IO $ \s -> case catch# (liftIO m s) (\exs -> liftIO (k exs) s)
111                           of STret s1 r -> (# s1, r #)
112 #endif
113
114 catch           :: IO a -> (IOError -> IO a) -> IO a 
115 catch m k       =  catchException m handler
116   where handler (IOException err) = k err
117         handler other             = throw other
118
119 catchNonIO      :: IO a -> (Exception -> IO a) -> IO a 
120 catchNonIO m k  =  catchException m handler
121   where handler (IOException err) = ioError err
122         handler other             = k other
123 \end{code}
124
125 Why is this stuff here?  To avoid recursive module dependencies of
126 course.
127
128 \begin{code}
129 ioError         :: IOError -> IO a 
130 ioError err     =  throw (IOException err)
131 \end{code}
132