From e121dccfabddad8c3dc4d1378f1b75b1d12ca789 Mon Sep 17 00:00:00 2001 From: simonm Date: Fri, 8 Jan 1999 11:37:27 +0000 Subject: [PATCH] [project @ 1999-01-08 11:37:27 by simonm] Doc changes for revised exception interface. --- ghc/docs/libraries/Exception.sgml | 188 ++++++++++++++++++++++++++----------- 1 file changed, 134 insertions(+), 54 deletions(-) diff --git a/ghc/docs/libraries/Exception.sgml b/ghc/docs/libraries/Exception.sgml index 1a2feca..4666f59 100644 --- a/ghc/docs/libraries/Exception.sgml +++ b/ghc/docs/libraries/Exception.sgml @@ -9,24 +9,24 @@ Exceptions are defined by the following (non-abstract) datatype: data Exception - = IOException IOError -- IO exceptions (from 'fail') - | ArithException ArithError -- 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 - | ExternalException ExtError -- External exceptions + = 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 ArithError +data ArithException = Overflow | Underflow | LossOfPrecision @@ -37,19 +37,20 @@ instance Eq ArithError instance Ord ArithError instance Show ArithError -data ExtError +data AsyncException = StackOverflow | HeapOverflow | ThreadKilled + deriving (Eq, Ord) -instance Eq ExtError -instance Ord ExtError -instance Show ExtError +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 external exceptions. +the arithmetic or the async exceptions. Exceptions may be thrown explicitly from anywhere: @@ -57,53 +58,92 @@ Exceptions may be thrown explicitly from anywhere: throw :: Exception -> a -Exceptions may be caught and examined in the The + +There are several functions for catching and examining exceptions; all +of them may only be used from within the -catch :: IO a -> (Exception -> IO a) -> IO a -catchIO :: IO a -> (IOError -> IO a) -> IO a -catchArith :: IO a -> (ArithError -> IO a) -> IO a -catchError :: IO a -> (String -> IO a) -> IO a +tryAll :: a -> IO (Either Exception a) +tryAllIO :: IO a -> IO (Either Exception a) +try :: (Exception -> Maybe b) -> a -> IO (Either b a) +tryIO :: (Exception -> Maybe b) -> IO a -> IO (Either b a) + -getException :: a -> IO (Maybe Exception) -getExceptionIO :: IO a -> IO (Either Exception a) +The simplest version is +justIoErrors :: Exception -> Maybe IOError +justArithExceptions :: Exception -> Maybe ArithException +justErrors :: Exception -> Maybe String +justDynExceptions :: Exception -> Maybe Dynamic +justAssertions :: Exception -> Maybe String +justAsyncExceptions :: Exception -> Maybe AsyncException + + +For example, to catch just calls to 'error' we could use something +like + + + result <- try justErrors thing_to_try -Each of the functions The + +The +catchAll :: a -> (Exception -> IO a) -> IO a +catchAllIO :: IO a -> (Exception -> IO a) -> IO a +catch :: (Exception -> Maybe b) -> a -> (b -> IO a) -> IO a +catchIO :: (Exception -> Maybe b) -> IO a -> (b -> IO a) -> IO a + + +The difference is that instead of returning an

@@ -123,3 +163,43 @@ The Other Utilities + +The + bracket :: IO a -> (a -> IO b) -> (a -> IO c) -> IO c + bracket_ :: IO a -> IO b -> IO c -> IO c + finally :: IO a -> IO b -> IO b + + +For example, to open a file, do some work on it and then close it +again, we might use something like: + + +process_file = bracket (openFile "filename") closeFile + (do + ... + ) + + + +withFile name = bracket (openFile name) closeFile + + +The