Next Previous Contents

6. Exception

The Exception library provides an interface for raising and catching both built-in and user defined exceptions.

Exceptions are defined by the following (non-abstract) datatype:

data Exception
  = IOException         IOError         -- IO exceptions (from 'fail')
  | ArithException      ArithException  -- Arithmetic exceptions
  | ErrorCall           String          -- Calls to 'error'
  | NoMethodError       String          -- A non-existent method was invoked
  | PatternMatchFail    String          -- A pattern match failed
  | NonExhaustiveGuards String          -- A guard match failed
  | RecSelError         String          -- Selecting a non-existent field
  | RecConError         String          -- Field missing in record construction
  | RecUpdError         String          -- Record doesn't contain updated field
  | AssertionFailed     String          -- Assertions
  | DynException        Dynamic         -- Dynamic exceptions
  | AsyncException      AsyncException  -- Externally generated errors

instance Eq   Exception
instance Ord  Exception
instance Show Exception

data ArithException
  = Overflow
  | Underflow
  | LossOfPrecision
  | DivideByZero
  | Denormal

instance Eq   ArithError
instance Ord  ArithError
instance Show ArithError

data AsyncException
  = StackOverflow
  | HeapOverflow
  | ThreadKilled
  deriving (Eq, Ord)

instance Eq   AsyncException
instance Ord  AsyncException
instance Show AsyncException

An implementation should raise the appropriate exception when one of the above conditions arises. Note: GHC currently doesn't generate the arithmetic or the async exceptions.

Exceptions may be thrown explicitly from anywhere:

throw :: Exception -> a


Next Previous Contents