Community
Modules 24

Control.Monad.Trans.Except

experimental

This monad transformer extends a monad with the ability to throw and catch exceptions.

A sequence of actions terminates normally, producing a value, only if none of the actions in the sequence throws an exception. If one throws an exception, the rest of the sequence is skipped and the composite action exits with that exception.

If the value of the exception is not required, the variant in Control.Monad.Trans.Maybe may be used instead.

The Except monad

type Except e = ExceptT e Identity

The parameterizable exception monad, which is strict.

Computations are either exceptions (of any type) or normal values. These computations are plain values, and are unrelated to the Control.Exception mechanism, which is tied to the IO monad.

The return function returns a normal value, while >>= exits on the first exception. For a variant that continues after an error and collects all the errors, see Errors.

except :: (Monad m) => Either e a -> ExceptT e m a

Constructor for computations in the exception monad. (The inverse of runExcept).

runExcept :: Except e a -> Either e a

Extractor for computations in the exception monad. (The inverse of except).

mapExcept :: (Either e a -> Either e' b) -> Except e a -> Except e' b

Map the unwrapped computation using the given function.

  • runExcept (mapExcept f m) = f (runExcept m)

withExcept :: (e -> e') -> Except e a -> Except e' a

Transform any exceptions thrown by the computation using the given function (a specialization of withExceptT).

The ExceptT monad transformer

newtype ExceptT e m a

A monad transformer that adds exceptions to other monads.

ExceptT constructs a strict monad parameterized over two things:

  • e - An arbitrary exception type.

  • m - The inner monad.

The monadic computations are a plain values. They are unrelated to the Control.Exception mechanism, which is tied to the IO monad.

The return function yields a computation that produces the given value, while >>= sequences two subcomputations, exiting on the first exception.

ExceptT :: m (Either e a) -> ExceptT e m a

mapExceptT :: (m (Either e a) -> n (Either e' b)) -> ExceptT e m a -> ExceptT e' n b

Map the unwrapped computation using the given function.

  • runExceptT (mapExceptT f m) = f (runExceptT m)

withExceptT :: (Functor m) => (e -> e') -> ExceptT e m a -> ExceptT e' m a

Transform any exceptions thrown by the computation using the given function.

Exception operations

throwE :: (Monad m) => e -> ExceptT e m a

Signal an exception value e.

  • runExceptT (throwE e) = return (Left e)

  • throwE e >>= m = throwE e

catchE :: (Monad m) => ExceptT e m a -> (e -> ExceptT e' m a) -> ExceptT e' m a

Handle an exception.

  • catchE (lift m) h = lift m

  • catchE (throwE e) h = h e

the inner computation

(e -> ExceptT e' m a)

a handler for exceptions in the inner computation

handleE :: Monad m => (e -> ExceptT e' m a) -> ExceptT e m a -> ExceptT e' m a

The same as flip catchE, which is useful in situations where the code for the handler is shorter.

tryE :: Monad m => ExceptT e m a -> ExceptT e m (Either e a)

Similar to catchE, but returns an Either result which is (Right a) if no exception was thown, or (Left ex) if an exception ex was thrown.

finallyE :: Monad m => ExceptT e m a -> ExceptT e m () -> ExceptT e m a

finallyE a b executes computation a followed by computation b, even if a exits early by throwing an exception. In the latter case, the exception is re-thrown after b has been executed.

onE :: (Monad m) => ExceptT e m a -> ExceptT e m b -> ExceptT e m a

If the first action succeeds, return its value, ignoring the second action. If the first action throws an exception, run the second action and then throw an exception, either the one thrown by the second action, if any, or the one thrown by the first action.

Lifting other operations

liftCallCC :: CallCC m (Either e a) (Either e b) -> CallCC (ExceptT e m) a b

Lift a callCC operation to the new monad.

liftListen :: (Monad m) => Listen w m (Either e a) -> Listen w (ExceptT e m) a

Lift a listen operation to the new monad.

liftPass :: (Monad m) => Pass w m (Either e a) -> Pass w (ExceptT e m) a

Lift a pass operation to the new monad.

Instances

23
  • instance (Generic e, Generic m, Generic a) => Generic (ExceptT e m a)
  • instance (Generic1 e, Generic1 m, Generic1 a) => Generic1 (ExceptT e m a)
  • instance (Eq e, Eq1 m) => Eq1 (ExceptT e m)
  • instance (Ord e, Ord1 m) => Ord1 (ExceptT e m)
  • instance (Read e, Read1 m) => Read1 (ExceptT e m)
  • instance (Show e, Show1 m) => Show1 (ExceptT e m)
  • instance (Eq e, Eq1 m, Eq a) => Eq (ExceptT e m a)
  • instance (Ord e, Ord1 m, Ord a) => Ord (ExceptT e m a)
  • instance (Read e, Read1 m, Read a) => Read (ExceptT e m a)
  • instance (Show e, Show1 m, Show a) => Show (ExceptT e m a)
  • instance (Functor m) => Functor (ExceptT e m)
  • instance (Foldable f) => Foldable (ExceptT e f)
  • instance (Traversable f) => Traversable (ExceptT e f)
  • instance (Functor m, Monad m) => Applicative (ExceptT e m)
  • instance (Functor m, Monad m, Monoid e) => Alternative (ExceptT e m)
  • instance (Monad m) => Monad (ExceptT e m)
  • instance (Fail.MonadFail m) => Fail.MonadFail (ExceptT e m)
  • instance (Monad m, Monoid e) => MonadPlus (ExceptT e m)
  • instance (MonadFix m) => MonadFix (ExceptT e m)
  • instance MonadTrans (ExceptT e)
  • instance (MonadIO m) => MonadIO (ExceptT e m)
  • instance (MonadZip m) => MonadZip (ExceptT e m)
  • instance Contravariant m => Contravariant (ExceptT e m)
Maintainer
R.Paterson@city.ac.uk
Portability
portable
Copyright
(C) 2013 Ross Paterson
License
BSD-style (see the file LICENSE)
Build record and source files

Community contribution. The service has not verified this documentation.

Target: Linux on x86-64. Some inferred types and resolved links are not available.

Generator: aihc-haddock-858b1951-web-1

Metadata snapshot: ac77ab7d2b1e88487eeab41661abedb7bfb8bc846deb92e7874c08d166bff65d

Open the dependency plan · Download the documentation model

Import a package

Enter a package name and an exact version from Hackage. The site contains imported versions only.