On this page
Control.Monad.Catch
| Copyright | (C) Edward Kmett 2013-2015 (c) Google Inc. 2012 |
|---|---|
| License | BSD-style (see the file LICENSE) |
| Maintainer | Edward Kmett <ekmett@gmail.com> |
| Stability | experimental |
| Portability | non-portable |
| Safe Haskell | Trustworthy |
| Language | Haskell2010 |
Description
This module supports monads that can throw extensible exceptions. The exceptions are the very same from Control.Exception, and the operations offered very similar, but here they are not limited to IO.
This code is in the style of both transformers and mtl, and is compatible with them, though doesn't mimic the module structure or offer the complete range of features in those packages.
This is very similar to ErrorT and MonadError, but based on features of Control.Exception. In particular, it handles the complex case of asynchronous exceptions by including mask in the typeclass. Note that the extensible exceptions feature relies on the RankNTypes language extension.
Typeclass
The mtl style typeclass
class Monad m => MonadThrow m where Source
A class for monads in which exceptions may be thrown.
Instances should obey the following law:
throwM e >> x = throwM e
In other words, throwing an exception short-circuits the rest of the monadic computation.
Methods
throwM :: Exception e => e -> m a Source
Throw an exception. Note that this throws when this action is run in the monad m, not when it is applied. It is a generalization of Control.Exception's throwIO.
Should satisfy the law:
throwM e >> f = throwM e
Instances
class MonadThrow m => MonadCatch m where Source
A class for monads which allow exceptions to be caught, in particular exceptions which were thrown by throwM.
Instances should obey the following law:
catch (throwM e) f = f e
Note that the ability to catch an exception does not guarantee that we can deal with all possible exit points from a computation. Some monads, such as continuation-based stacks, allow for more than just a success/failure strategy, and therefore catch cannot be used by those monads to properly implement a function such as finally. For more information, see MonadMask.
Methods
catch :: Exception e => m a -> (e -> m a) -> m a Source
Provide a handler for exceptions thrown during execution of the first action. Note that type of the type of the argument to the handler will constrain which exceptions are caught. See Control.Exception's catch.
Instances
| MonadCatch STM Source | |
| MonadCatch IO Source | |
| e ~ SomeException => MonadCatch (Either e) Source | Since: exceptions-0.8.3 |
| Monad m => MonadCatch (CatchT m) Source | |
| MonadCatch m => MonadCatch (ListT m) Source | |
| MonadCatch m => MonadCatch (MaybeT m) Source | Catches exceptions from the base monad. |
| (Error e, MonadCatch m) => MonadCatch (ErrorT e m) Source | Catches exceptions from the base monad. |
| MonadCatch m => MonadCatch (ExceptT e m) Source | Catches exceptions from the base monad. |
| MonadCatch m => MonadCatch (IdentityT m) Source | |
| MonadCatch m => MonadCatch (ReaderT r m) Source | |
| MonadCatch m => MonadCatch (StateT s m) Source | |
| MonadCatch m => MonadCatch (StateT s m) Source | |
| (MonadCatch m, Monoid w) => MonadCatch (WriterT w m) Source | |
| (MonadCatch m, Monoid w) => MonadCatch (WriterT w m) Source | |
| (MonadCatch m, Monoid w) => MonadCatch (RWST r w s m) Source | |
| (MonadCatch m, Monoid w) => MonadCatch (RWST r w s m) Source | |
class MonadCatch m => MonadMask m where Source
A class for monads which provide for the ability to account for all possible exit points from a computation, and to mask asynchronous exceptions. Continuation-based monads are invalid instances of this class.
Instances should ensure that, in the following code:
fg = f `finally` g
The action g is called regardless of what occurs within f, including async exceptions. Some monads allow f to abort the computation via other effects than throwing an exception. For simplicity, we will consider aborting and throwing an exception to be two forms of "throwing an error".
If f and g both throw an error, the error thrown by fg depends on which errors we're talking about. In a monad transformer stack, the deeper layers override the effects of the inner layers; for example, ExceptT e1 (Except e2) a represents a value of type Either e2 (Either e1 a), so throwing both an e1 and an e2 will result in Left e2. If f and g both throw an error from the same layer, instances should ensure that the error from g wins.
Effects other than throwing an error are also overridden by the deeper layers. For example, StateT s Maybe a represents a value of type s -> Maybe (a, s), so if an error thrown from f causes this function to return Nothing, any changes to the state which f also performed will be erased. As a result, g will see the state as it was before f. Once g completes, f's error will be rethrown, so g' state changes will be erased as well. This is the normal interaction between effects in a monad transformer stack.
By contrast, lifted-base's version of finally always discards all of g's non-IO effects, and g never sees any of f's non-IO effects, regardless of the layer ordering and regardless of whether f throws an error. This is not the result of interacting effects, but a consequence of MonadBaseControl's approach.
Methods
mask :: ((forall a. m a -> m a) -> m b) -> m b Source
Runs an action with asynchronous exceptions disabled. The action is provided a method for restoring the async. environment to what it was at the mask call. See Control.Exception's mask.
uninterruptibleMask :: ((forall a. m a -> m a) -> m b) -> m b Source
Like mask, but the masked computation is not interruptible (see Control.Exception's uninterruptibleMask. WARNING: Only use if you need to mask exceptions around an interruptible operation AND you can guarantee the interruptible operation will only block for a short period of time. Otherwise you render the program/thread unresponsive and/or unkillable.
Arguments
| :: m a | acquire some resource |
| -> (a -> ExitCase b -> m c) | release the resource, observing the outcome of the inner action |
| -> (a -> m b) | inner action to perform with the resource |
| -> m (b, c) |