Portability | non-portable |
---|---|
Stability | experimental |
Maintainer | Andy Sonnenburg <andy22286@gmail.com> |
Safe Haskell | Safe-Inferred |
- Computation type:
- Computations which may fail or throw exceptions; and computations which may catch failures and thrown exceptions.
- Binding strategy:
-
Failure records information about the cause/location of the failure. Failure
values bypass the bound function; other values are used as inputs to the bound
function (same as
).MonadError
- Useful for:
-
Building computations from sequences of functions that may fail; and using
exception handling to structure error handling. The handler may or may not
throw an exception, which does not have to be of the same type as the original
thrown exception (see
).mapE
- Zero and plus:
-
Zero is represented by an empty error, and the plus operation executes its
second argument if the first fails (same as
).MonadError
- Example type:
-
Either
String
a
The Throw and Catch monads.
- class Monad m => MonadThrow e m | m -> e where
- throw :: e -> m a
- class (MonadThrow e m, Monad n) => MonadCatch e m n | n e -> m where
- catch :: m a -> (e -> n a) -> n a
- mapE :: (MonadCatch e m n, MonadThrow e' n) => (e -> e') -> m a -> n a
Documentation
class Monad m => MonadThrow e m | m -> e whereSource
The strategy of combining computations that can throw exceptions.
Is parameterized over the type of error information and the monad type
constructor. It is common to use
. In some cases you will
have to define an instance of Either
String
, though rarely a definition of
MonadThrow
throw
Is used within a monadic computation to begin exception processing. If
(
, then MonadThrow
e n, MonadTrans
t) => t n ~ m
is the default definition.
throw
= lift
.
throw
class (MonadThrow e m, Monad n) => MonadCatch e m n | n e -> m whereSource
The strategy of combining computations that can handle thrown exceptions, as well as throwing exceptions in the original computation.
Is parameterized over the type of error information and the original monad type constructor, as well as the handler monad type constructor. The handler monad type constructor commonly differs from the original monad type constructor due to a change in the type of the error information.
catch :: m a -> (e -> n a) -> n aSource
A handler function to handle thrown values and return to normal execution. A common idiom is:
do { action1; action2; action3 } `catch` handler
where the action
functions can call throw
.
Note that handler
and the do-block must have the same return type.
mapE :: (MonadCatch e m n, MonadThrow e' n) => (e -> e') -> m a -> n aSource
Map the thrown value using the given function