haskell / 9 / libraries / exceptions-0.10.5 / control-monad-catch.html

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
Instances details
MonadThrow STM Source
Instance details

Defined in Control.Monad.Catch

Methods

throwM :: Exception e => e -> STM a Source

MonadThrow IO Source
Instance details

Defined in Control.Monad.Catch

Methods

throwM :: Exception e => e -> IO a Source

MonadThrow Q Source
Instance details

Defined in Control.Monad.Catch

Methods

throwM :: Exception e => e -> Q a Source

MonadThrow Maybe Source
Instance details

Defined in Control.Monad.Catch

Methods

throwM :: Exception e => e -> Maybe a Source

MonadThrow [] Source
Instance details

Defined in Control.Monad.Catch

Methods

throwM :: Exception e => e -> [a] Source

e ~ SomeException => MonadThrow (Either e) Source
Instance details

Defined in Control.Monad.Catch

Methods

throwM :: Exception e0 => e0 -> Either e a Source

MonadThrow (ST s) Source
Instance details

Defined in Control.Monad.Catch

Methods

throwM :: Exception e => e -> ST s a Source

Monad m => MonadThrow (CatchT m) Source
Instance details

Defined in Control.Monad.Catch.Pure

Methods

throwM :: Exception e => e -> CatchT m a Source

MonadThrow m => MonadThrow (ListT m) Source
Instance details

Defined in Control.Monad.Catch

Methods

throwM :: Exception e => e -> ListT m a Source

MonadThrow m => MonadThrow (MaybeT m) Source

Throws exceptions into the base monad.

Instance details

Defined in Control.Monad.Catch

Methods

throwM :: Exception e => e -> MaybeT m a Source

(Error e, MonadThrow m) => MonadThrow (ErrorT e m) Source

Throws exceptions into the base monad.

Instance details

Defined in Control.Monad.Catch

Methods

throwM :: Exception e0 => e0 -> ErrorT e m a Source

MonadThrow m => MonadThrow (ExceptT e m) Source

Throws exceptions into the base monad.

Instance details

Defined in Control.Monad.Catch

Methods

throwM :: Exception e0 => e0 -> ExceptT e m a Source

MonadThrow m => MonadThrow (IdentityT m) Source
Instance details

Defined in Control.Monad.Catch

Methods

throwM :: Exception e => e -> IdentityT m a Source

MonadThrow m => MonadThrow (ReaderT r m) Source
Instance details

Defined in Control.Monad.Catch

Methods

throwM :: Exception e => e -> ReaderT r m a Source

MonadThrow m => MonadThrow (StateT s m) Source
Instance details

Defined in Control.Monad.Catch

Methods

throwM :: Exception e => e -> StateT s m a Source

MonadThrow m => MonadThrow (StateT s m) Source
Instance details

Defined in Control.Monad.Catch

Methods

throwM :: Exception e => e -> StateT s m a Source

(MonadThrow m, Monoid w) => MonadThrow (WriterT w m) Source
Instance details

Defined in Control.Monad.Catch

Methods

throwM :: Exception e => e -> WriterT w m a Source

(MonadThrow m, Monoid w) => MonadThrow (WriterT w m) Source
Instance details

Defined in Control.Monad.Catch

Methods

throwM :: Exception e => e -> WriterT w m a Source

MonadThrow m => MonadThrow (ContT r m) Source
Instance details

Defined in Control.Monad.Catch

Methods

throwM :: Exception e => e -> ContT r m a Source

(MonadThrow m, Monoid w) => MonadThrow (RWST r w s m) Source
Instance details

Defined in Control.Monad.Catch

Methods

throwM :: Exception e => e -> RWST r w s m a Source

(MonadThrow m, Monoid w) => MonadThrow (RWST r w s m) Source
Instance details

Defined in Control.Monad.Catch

Methods

throwM :: Exception e => e -> RWST r w s m a Source

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
Instances details
MonadCatch STM Source
Instance details

Defined in Control.Monad.Catch

Methods

catch :: Exception e => STM a -> (e -> STM a) -> STM a Source

MonadCatch IO Source
Instance details

Defined in Control.Monad.Catch

Methods

catch :: Exception e => IO a -> (e -> IO a) -> IO a Source

e ~ SomeException => MonadCatch (Either e) Source

Since: exceptions-0.8.3

Instance details

Defined in Control.Monad.Catch

Methods

catch :: Exception e0 => Either e a -> (e0 -> Either e a) -> Either e a Source

Monad m => MonadCatch (CatchT m) Source
Instance details

Defined in Control.Monad.Catch.Pure

Methods

catch :: Exception e => CatchT m a -> (e -> CatchT m a) -> CatchT m a Source

MonadCatch m => MonadCatch (ListT m) Source
Instance details

Defined in Control.Monad.Catch

Methods

catch :: Exception e => ListT m a -> (e -> ListT m a) -> ListT m a Source

MonadCatch m => MonadCatch (MaybeT m) Source

Catches exceptions from the base monad.

Instance details

Defined in Control.Monad.Catch

Methods

catch :: Exception e => MaybeT m a -> (e -> MaybeT m a) -> MaybeT m a Source

(Error e, MonadCatch m) => MonadCatch (ErrorT e m) Source

Catches exceptions from the base monad.

Instance details

Defined in Control.Monad.Catch

Methods

catch :: Exception e0 => ErrorT e m a -> (e0 -> ErrorT e m a) -> ErrorT e m a Source

MonadCatch m => MonadCatch (ExceptT e m) Source

Catches exceptions from the base monad.

Instance details

Defined in Control.Monad.Catch

Methods

catch :: Exception e0 => ExceptT e m a -> (e0 -> ExceptT e m a) -> ExceptT e m a Source

MonadCatch m => MonadCatch (IdentityT m) Source
Instance details

Defined in Control.Monad.Catch

Methods

catch :: Exception e => IdentityT m a -> (e -> IdentityT m a) -> IdentityT m a Source

MonadCatch m => MonadCatch (ReaderT r m) Source
Instance details

Defined in Control.Monad.Catch

Methods

catch :: Exception e => ReaderT r m a -> (e -> ReaderT r m a) -> ReaderT r m a Source

MonadCatch m => MonadCatch (StateT s m) Source
Instance details

Defined in Control.Monad.Catch

Methods

catch :: Exception e => StateT s m a -> (e -> StateT s m a) -> StateT s m a Source

MonadCatch m => MonadCatch (StateT s m) Source
Instance details

Defined in Control.Monad.Catch

Methods

catch :: Exception e => StateT s m a -> (e -> StateT s m a) -> StateT s m a Source

(MonadCatch m, Monoid w) => MonadCatch (WriterT w m) Source
Instance details

Defined in Control.Monad.Catch

Methods

catch :: Exception e => WriterT w m a -> (e -> WriterT w m a) -> WriterT w m a Source

(MonadCatch m, Monoid w) => MonadCatch (WriterT w m) Source
Instance details

Defined in Control.Monad.Catch

Methods

catch :: Exception e => WriterT w m a -> (e -> WriterT w m a) -> WriterT w m a Source

(MonadCatch m, Monoid w) => MonadCatch (RWST r w s m) Source
Instance details

Defined in Control.Monad.Catch

Methods

catch :: Exception e => RWST r w s m a -> (e -> RWST r w s m a) -> RWST r w s m a Source

(MonadCatch m, Monoid w) => MonadCatch (RWST r w s m) Source
Instance details

Defined in Control.Monad.Catch

Methods

catch :: Exception e => RWST r w s m a -> (e -> RWST r w s m a) -> RWST r w s m a 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.

generalBracket Source

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)