Control.Monad.Exception.Base
Description
A Monad Transformer for explicitly typed checked exceptions.
The exceptions thrown by a computation are inferred by the typechecker
and appear in the type signature of the computation as Throws
constraints.
Exceptions are defined using the extensible exceptions framework of Marlow (documented in Control.Exception):
- An Extensible Dynamically-Typed Hierarchy of Exceptions, by Simon Marlow, in Haskell '06.
Example
data DivideByZero = DivideByZero deriving (Show, Typeable) data SumOverflow = SumOverflow deriving (Show, Typeable)
instance Exception DivideByZero instance Exception SumOverflow
data Expr = Add Expr Expr | Div Expr Expr | Val Double
eval (Val x) = return x eval (Add a1 a2) = do v1 <- eval a1 v2 <- eval a2 let sum = v1 + v2 if sum < v1 || sum < v2 then throw SumOverflow else return sum eval (Div a1 a2) = do v1 <- eval a1 v2 <- eval a2 if v2 == 0 then throw DivideByZero else return (v1 / v2)
GHCi infers the following types
eval :: (Throws DivideByZero l, Throws SumOverflow l) => Expr -> EM l Double eval `catch` \ (e::DivideByZero) -> return (-1) :: Throws SumOverflow l => Expr -> EM l Double runEM(eval `catch` \ (e::SomeException) -> return (-1)) :: Expr -> Double
- type CallTrace = [String]
- newtype EMT l m a = EMT {
- unEMT :: m (Either (CallTrace, CheckedException l) a)
- tryEMT :: Monad m => EMT AnyException m a -> m (Either SomeException a)
- tryEMTWithLoc :: Monad m => EMT AnyException m a -> m (Either (CallTrace, SomeException) a)
- runEMT_gen :: forall l m a. Monad m => EMT l m a -> m a
- data AnyException
- data NoExceptions
- data ParanoidMode
- runEMT :: Monad m => EMT NoExceptions m a -> m a
- runEMTParanoid :: Monad m => EMT ParanoidMode m a -> m a
- throw :: (Exception e, Throws e l, Monad m) => e -> EMT l m a
- rethrow :: (Throws e l, Monad m) => CallTrace -> e -> EMT l m a
- catch :: (Exception e, Monad m) => EMT (Caught e l) m a -> (e -> EMT l m a) -> EMT l m a
- catchWithSrcLoc :: (Exception e, Monad m) => EMT (Caught e l) m a -> (CallTrace -> e -> EMT l m a) -> EMT l m a
- finally :: Monad m => EMT l m a -> EMT l m b -> EMT l m a
- onException :: Monad m => EMT l m a -> EMT l m b -> EMT l m a
- bracket :: Monad m => EMT l m a -> (a -> EMT l m b) -> (a -> EMT l m c) -> EMT l m c
- wrapException :: (Exception e, Throws e' l, Monad m) => (e -> e') -> EMT (Caught e l) m a -> EMT l m a
- showExceptionWithTrace :: Exception e => [String] -> e -> String
- class Exception e => UncaughtException e
- type EM l = EMT l Identity
- tryEM :: EM AnyException a -> Either SomeException a
- tryEMWithLoc :: EM AnyException a -> Either (CallTrace, SomeException) a
- runEM :: EM NoExceptions a -> a
- runEMParanoid :: EM ParanoidMode a -> a
- newtype Identity a = Identity {
- runIdentity :: a
- data FailException = FailException String
- data MonadZeroException = MonadZeroException
- mplusDefault :: Monad m => EMT l m a -> EMT l m a -> EMT l m a
- mapLeft :: (a -> b) -> Either a r -> Either b r
Documentation
A Monad Transformer for explicitly typed checked exceptions.
Constructors
EMT | |
Fields
|
Instances
(Exception e, Throws e l, Failure e m, Monad m) => WrapFailure e (EMT l m) | |
(Exception e, Throws e l, Monad m) => Failure e (EMT l m) | |
(Exception e, Monad m) => MonadCatch e (EMT (Caught e l) m) (EMT l m) | |
Monad m => Monad (EMT l m) | |
Monad m => Functor (EMT l m) | |
MonadFix m => MonadFix (EMT l m) | |
Monad m => Applicative (EMT l m) | |
Monad m => MonadLoc (EMT l m) |
tryEMT :: Monad m => EMT AnyException m a -> m (Either SomeException a)Source
Run a computation explicitly handling exceptions
tryEMTWithLoc :: Monad m => EMT AnyException m a -> m (Either (CallTrace, SomeException) a)Source
runEMT_gen :: forall l m a. Monad m => EMT l m a -> m aSource
data AnyException Source
Instances
Exception e => Throws e AnyException |
data NoExceptions Source
Instances
UncaughtException e => Throws e NoExceptions |
data ParanoidMode Source
runEMT :: Monad m => EMT NoExceptions m a -> m aSource
Run a safe computation
runEMTParanoid :: Monad m => EMT ParanoidMode m a -> m aSource
Run a safe computation checking even unchecked (UncaughtException
) exceptions
rethrow :: (Throws e l, Monad m) => CallTrace -> e -> EMT l m aSource
Rethrow an exception keeping the call trace
catch :: (Exception e, Monad m) => EMT (Caught e l) m a -> (e -> EMT l m a) -> EMT l m aSource
The catch primitive
catchWithSrcLoc :: (Exception e, Monad m) => EMT (Caught e l) m a -> (CallTrace -> e -> EMT l m a) -> EMT l m aSource
Like catch
but makes the call trace available
finally :: Monad m => EMT l m a -> EMT l m b -> EMT l m aSource
Sequence two computations discarding the result of the second one. If the first computation rises an exception, the second computation is run and then the exception is rethrown.
onException :: Monad m => EMT l m a -> EMT l m b -> EMT l m aSource
Like finally, but performs the second computation only when the first one rises an exception
wrapException :: (Exception e, Throws e' l, Monad m) => (e -> e') -> EMT (Caught e l) m a -> EMT l m aSource
Capture an exception e, wrap it, and rethrow. Keeps the original monadic call trace.
showExceptionWithTrace :: Exception e => [String] -> e -> StringSource
class Exception e => UncaughtException e Source
UncaughtException models unchecked exceptions
In order to declare an unchecked exception E
,
all that is needed is to make e
an instance of UncaughtException
instance UncaughtException E
Note that declaring an exception E as unchecked does not automatically turn its children unchecked too. This is a shortcoming of the current encoding.
Instances
tryEM :: EM AnyException a -> Either SomeException aSource
Run a computation explicitly handling exceptions
tryEMWithLoc :: EM AnyException a -> Either (CallTrace, SomeException) aSource
runEM :: EM NoExceptions a -> aSource
Run a safe computation
runEMParanoid :: EM ParanoidMode a -> aSource
Run a computation checking even unchecked (UncaughtExceptions
) exceptions
Constructors
Identity | |
Fields
|
data MonadZeroException Source
MonadZeroException
is thrown by MonadPlus mzero
Constructors
MonadZeroException |