[project @ 2000-01-30 10:25:27 by simonmar]
[ghc-hetmet.git] / ghc / lib / std / PrelException.lhs
1 % -----------------------------------------------------------------------------
2 % $Id: PrelException.lhs,v 1.11 2000/01/30 10:25:28 simonmar 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 PrelShow
17 import PrelIOBase
18 import PrelST           ( STret(..) )
19 import PrelDynamic
20 import PrelGHC
21 #endif
22 \end{code}
23
24 -----------------------------------------------------------------------------
25 Exception datatype and operations.
26
27 \begin{code}
28 data Exception
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
41   | NonTermination
42
43 data ArithException
44   = Overflow
45   | Underflow
46   | LossOfPrecision
47   | DivideByZero
48   | Denormal
49   deriving (Eq, Ord)
50
51 data AsyncException
52   = StackOverflow
53   | HeapOverflow
54   | ThreadKilled
55   deriving (Eq, Ord)
56
57 stackOverflow, heapOverflow :: Exception -- for the RTS
58 stackOverflow = AsyncException StackOverflow
59 heapOverflow  = AsyncException HeapOverflow
60
61 instance Show ArithException where
62   showsPrec _ Overflow        = showString "arithmetic overflow"
63   showsPrec _ Underflow       = showString "arithmetic underflow"
64   showsPrec _ LossOfPrecision = showString "loss of precision"
65   showsPrec _ DivideByZero    = showString "divide by zero"
66   showsPrec _ Denormal        = showString "denormal"
67
68 instance Show AsyncException where
69   showsPrec _ StackOverflow   = showString "stack overflow"
70   showsPrec _ HeapOverflow    = showString "heap overflow"
71   showsPrec _ ThreadKilled    = showString "thread killed"
72
73 instance Show Exception where
74   showsPrec _ (IOException err)          = shows err
75   showsPrec _ (ArithException err)       = shows err
76   showsPrec _ (ErrorCall err)            = showString err
77   showsPrec _ (NoMethodError err)        = showString err
78   showsPrec _ (PatternMatchFail err)     = showString err
79   showsPrec _ (NonExhaustiveGuards err)  = showString err
80   showsPrec _ (RecSelError err)          = showString err
81   showsPrec _ (RecConError err)          = showString err
82   showsPrec _ (RecUpdError err)          = showString err
83   showsPrec _ (AssertionFailed err)      = showString err
84   showsPrec _ (AsyncException e)         = shows e
85   showsPrec _ (DynException _err)        = showString "unknown exception"
86   showsPrec _ (NonTermination)           = showString "<<loop>>"
87
88 -- Primitives:
89
90 throw :: Exception -> a
91
92 #ifdef __HUGS__
93 throw = primRaise
94 #else
95 throw exception = raise# exception
96 #endif
97 \end{code}
98
99 catchException used to handle the passing around of the state to the
100 action and the handler.  This turned out to be a bad idea - it meant
101 that we had to wrap both arguments in thunks so they could be entered
102 as normal (remember IO returns an unboxed pair...).
103
104 Now catch# has type
105
106     catch# :: IO a -> (b -> IO a) -> IO a
107
108 (well almost; the compiler doesn't know about the IO newtype so we
109 have to work around that in the definition of catchException below).
110
111 \begin{code}
112 catchException :: IO a -> (Exception -> IO a) -> IO a
113 #ifdef __HUGS__
114 catchException m k =  ST (\s -> unST m s `primCatch'` \ err -> unST (k err) s)
115 #else
116 catchException (IO m) k =  IO $ \s -> catch# m (\ex -> unIO (k ex)) s
117 #endif
118
119 catch           :: IO a -> (IOError -> IO a) -> IO a 
120 catch m k       =  catchException m handler
121   where handler (IOException err) = k err
122         handler other             = throw other
123
124 catchNonIO      :: IO a -> (Exception -> IO a) -> IO a 
125 catchNonIO m k  =  catchException m handler
126   where handler (IOException err) = ioError err
127         handler other             = k other
128 \end{code}
129
130
131 Why is this stuff here?  To avoid recursive module dependencies of
132 course.
133
134 \begin{code}
135 ioError         :: IOError -> IO a 
136 ioError err     =  throw (IOException err)
137 \end{code}
138