Control.Monad.Except

Copyright (c) Michael Weber <[email protected]> 2001
(c) Jeff Newbern 2003-2006
(c) Andriy Palamarchuk 2006
License BSD-style (see the file LICENSE)
Maintainer [email protected]
Stability experimental
Portability non-portable (multi-parameter type classes)
Safe Haskell Safe
Language Haskell2010

Description

Computation type:
Computations which may fail or throw 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.
Useful for:
Building computations from sequences of functions that may fail or using exception handling to structure error handling.
Example type:
Either String a

The Error monad (also called the Exception monad).

Since: mtl-2.2.1

Warning

Please do not confuse ExceptT and throwError with Exception / SomeException and catch, respectively. The latter are for exceptions built into GHC, by default, and are mostly used from within the IO monad. They do not interact with the "exceptions" in this package at all. This package allows you to define a new kind of exception control mechanism which does not necessarily need your code to be placed in the IO monad.

In short, all "catching" mechanisms in this library will be unable to catch exceptions thrown by functions in the Control.Exception module, and vice-versa.

class Monad m => MonadError e m | m -> e where Source

The strategy of combining computations that can throw exceptions by bypassing bound functions from the point an exception is thrown to the point that it is handled.

Is parameterized over the type of error information and the monad type constructor. It is common to use Either String as the monad type constructor for an error monad in which error descriptions take the form of strings. In that case and many other common cases the resulting monad is already defined as an instance of the MonadError class. You can also define your own error type and/or use a monad type constructor other than Either String or Either IOError. In these cases you will have to explicitly define instances of the MonadError class. (If you are using the deprecated Control.Monad.Error or Control.Monad.Trans.Error, you may also have to define an Error instance.)

Methods

throwError :: e -> m a Source

Is used within a monadic computation to begin exception processing.

catchError :: m a -> (e -> m a) -> m a Source

A handler function to handle previous errors and return to normal execution. A common idiom is:

do { action1; action2; action3 } `catchError` handler

where the action functions can call throwError. Note that handler and the do-block must have the same return type.

Instances
Instances details
MonadError () Maybe

Since: mtl-2.2.2

Instance details

Defined in Control.Monad.Error.Class

Methods

throwError :: () -> Maybe a Source

catchError :: Maybe a -> (() -> Maybe a) -> Maybe a Source

MonadError IOException IO
Instance details

Defined in Control.Monad.Error.Class

Methods

throwError :: IOException -> IO a Source

catchError :: IO a -> (IOException -> IO a) -> IO a Source

MonadError e m => MonadError e (MaybeT m)
Instance details

Defined in Control.Monad.Error.Class

Methods

throwError :: e -> MaybeT m a Source

catchError :: MaybeT m a -> (e -> MaybeT m a) -> MaybeT m a Source

MonadError e m => MonadError e (ListT m)
Instance details

Defined in Control.Monad.Error.Class

Methods

throwError :: e -> ListT m a Source

catchError :: ListT m a -> (e -> ListT m a) -> ListT m a Source

MonadError e (Either e)
Instance details

Defined in Control.Monad.Error.Class

Methods

throwError :: e -> Either e a Source

catchError :: Either e a -> (e -> Either e a) -> Either e a Source

(Monoid w, MonadError e m) => MonadError e (WriterT w m)
Instance details

Defined in Control.Monad.Error.Class

Methods

throwError :: e -> WriterT w m a Source

catchError :: WriterT w m a -> (e -> WriterT w m a) -> WriterT w m a Source

(Monoid w, MonadError e m) => MonadError e (WriterT w m)
Instance details

Defined in Control.Monad.Error.Class

Methods

throwError :: e -> WriterT w m a Source

catchError :: WriterT w m a -> (e -> WriterT w m a) -> WriterT w m a Source

MonadError e m => MonadError e (StateT s m)
Instance details

Defined in Control.Monad.Error.Class

Methods

throwError :: e -> StateT s m a Source

catchError :: StateT s m a -> (e -> StateT s m a) -> StateT s m a Source

MonadError e m => MonadError e (StateT s m)
Instance details

Defined in Control.Monad.Error.Class

Methods

throwError :: e -> StateT s m a Source

catchError :: StateT s m a -> (e -> StateT s m a) -> StateT s m a Source

MonadError e m => MonadError e (ReaderT r m)
Instance details

Defined in Control.Monad.Error.Class

Methods

throwError :: e -> ReaderT r m a Source

catchError :: ReaderT r m a -> (e -> ReaderT r m a) -> ReaderT r m a Source

MonadError e m => MonadError e (IdentityT m)
Instance details

Defined in Control.Monad.Error.Class

Methods

throwError :: e -> IdentityT m a Source

catchError :: IdentityT m a -> (e -> IdentityT m a) -> IdentityT m a Source

Monad m => MonadError e (ExceptT e m)

Since: mtl-2.2

Instance details

Defined in Control.Monad.Error.Class

Methods

throwError :: e -> ExceptT e m a Source

catchError :: ExceptT e m a -> (e -> ExceptT e m a) -> ExceptT e m a Source

(Monad m, Error e) => MonadError e (ErrorT e m)
Instance details

Defined in Control.Monad.Error.Class

Methods

throwError :: e -> ErrorT e m a Source

catchError :: ErrorT e m a -> (e -> ErrorT e m a) -> ErrorT e m a Source

(Monoid w, MonadError e m) => MonadError e (RWST r w s m)
Instance details

Defined in Control.Monad.Error.Class

Methods

throwError :: e -> RWST r w s m a Source

catchError :: RWST r w s m a -> (e -> RWST r w s m a) -> RWST r w s m a Source

(Monoid w, MonadError e m) => MonadError e (RWST r w s m)
Instance details

Defined in Control.Monad.Error.Class

Methods

throwError :: e -> RWST r w s m a Source

catchError :: RWST r w s m a -> (e -> RWST r w s m a) -> RWST r w s m a Source

liftEither :: MonadError e m => Either e a -> m a Source

Lifts an Either e into any MonadError e.

do { val <- liftEither =<< action1; action2 }

where action1 returns an Either to represent errors.

Since: mtl-2.2.2

The ExceptT monad transformer

newtype ExceptT e (m :: Type -> Type) a Source

A monad transformer that adds exceptions to other monads.

ExceptT constructs a monad parameterized over two things:

  • e - The exception type.
  • m - The inner monad.

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

Constructors

ExceptT (m (Either e a))
Instances
Instances details
MonadRWS r w s m => MonadRWS r w s (ExceptT e m)

Since: mtl-2.2

Instance details

Defined in Control.Monad.RWS.Class

Monad m => MonadError e (ExceptT e m)

Since: mtl-2.2

Instance details

Defined in Control.Monad.Error.Class

Methods

throwError :: e -> ExceptT e m a Source

catchError :: ExceptT e m a -> (e -> ExceptT e m a) -> ExceptT e m a Source

MonadReader r m => MonadReader r (ExceptT e m)

Since: mtl-2.2

Instance details

Defined in Control.Monad.Reader.Class

Methods

ask :: ExceptT e m r Source

local :: (r -> r) -> ExceptT e m a -> ExceptT e m a Source

reader :: (r -> a) -> ExceptT e m a Source

MonadState s m => MonadState s (ExceptT e m)

Since: mtl-2.2

Instance details

Defined in Control.Monad.State.Class

Methods

get :: ExceptT e m s Source

put :: s -> ExceptT e m () Source

state :: (s -> (a, s)) -> ExceptT e m a Source

MonadWriter w m => MonadWriter w (ExceptT e m)

Since: mtl-2.2

Instance details

Defined in Control.Monad.Writer.Class

Methods

writer :: (a, w) -> ExceptT e m a Source

tell :: w -> ExceptT e m () Source

listen :: ExceptT e m a -> ExceptT e m (a, w) Source

pass :: ExceptT e m (a, w -> w) -> ExceptT e m a Source

MonadTrans (ExceptT e)
Instance details

Defined in Control.Monad.Trans.Except

Methods

lift :: Monad m => m a -> ExceptT e m a Source

Monad m => Monad (ExceptT e m)
Instance details

Defined in Control.Monad.Trans.Except

Methods

(>>=) :: ExceptT e m a -> (a -> ExceptT e m b) -> ExceptT e m b Source

(>>) :: ExceptT e m a -> ExceptT e m b -> ExceptT e m b Source

return :: a -> ExceptT e m a Source

Functor m => Functor (ExceptT e m)
Instance details

Defined in Control.Monad.Trans.Except

Methods

fmap :: (a -> b) -> ExceptT e m a -> ExceptT e m b Source

(<$) :: a -> ExceptT e m b -> ExceptT e m a Source

MonadFix m => MonadFix (ExceptT e m)
Instance details

Defined in Control.Monad.Trans.Except

Methods

mfix :: (a -> ExceptT e m a) -> ExceptT e m a Source

MonadFail m => MonadFail (ExceptT e m)
Instance details

Defined in Control.Monad.Trans.Except

Methods

fail :: String -> ExceptT e m a Source

(Functor m, Monad m) => Applicative (ExceptT e m)
Instance details

Defined in Control.Monad.Trans.Except

Methods

pure :: a -> ExceptT e m a Source

(<*>) :: ExceptT e m (a -> b) -> ExceptT e m a -> ExceptT e m b Source

liftA2 :: (a -> b -> c) -> ExceptT e m a -> ExceptT e m b -> ExceptT e m c Source

(*>) :: ExceptT e m a -> ExceptT e m b -> ExceptT e m b Source

(<*) :: ExceptT e m a -> ExceptT e m b -> ExceptT e m a Source

Foldable f => Foldable (ExceptT e f)
Instance details

Defined in Control.Monad.Trans.Except

Methods

fold :: Monoid m => ExceptT e f m -> m Source

foldMap :: Monoid m => (a -> m) -> ExceptT e f a -> m Source

foldMap' :: Monoid m => (a -> m) -> ExceptT e f a -> m Source

foldr :: (a -> b -> b) -> b -> ExceptT e f a -> b Source

foldr' :: (a -> b -> b) -> b -> ExceptT e f a -> b Source

foldl :: (b -> a -> b) -> b -> ExceptT e f a -> b Source

foldl' :: (b -> a -> b) -> b -> ExceptT e f a -> b Source

foldr1 :: (a -> a -> a) -> ExceptT e f a -> a Source

foldl1 :: (a -> a -> a) -> ExceptT e f a -> a Source

toList :: ExceptT e f a -> [a] Source

null :: ExceptT e f a -> Bool Source

length :: ExceptT e f a -> Int Source

elem :: Eq a => a -> ExceptT e f a -> Bool Source

maximum :: Ord a => ExceptT e f a -> a Source

minimum :: Ord a => ExceptT e f a -> a Source

sum :: Num a => ExceptT e f a -> a Source

product :: Num a => ExceptT e f a -> a Source

Traversable f => Traversable (ExceptT e f)
Instance details

Defined in Control.Monad.Trans.Except

Methods

traverse :: Applicative f0 => (a -> f0 b) -> ExceptT e f a -> f0 (ExceptT e f b) Source

sequenceA :: Applicative f0 => ExceptT e f (f0 a) -> f0 (ExceptT e f a) Source

mapM :: Monad m => (a -> m b) -> ExceptT e f a -> m (ExceptT e f b) Source

sequence :: Monad m => ExceptT e f (m a) -> m (ExceptT e f a) Source

Contravariant m => Contravariant (ExceptT e m)
Instance details

Defined in Control.Monad.Trans.Except

Methods

contramap :: (a -> b) -> ExceptT e m b -> ExceptT e m a Source

(>$) :: b -> ExceptT e m b -> ExceptT e m a Source

(Eq e, Eq1 m) => Eq1 (ExceptT e m)
Instance details

Defined in Control.Monad.Trans.Except

Methods

liftEq :: (a -> b -> Bool) -> ExceptT e m a -> ExceptT e m b -> Bool Source

(Ord e, Ord1 m) => Ord1 (ExceptT e m)
Instance details

Defined in Control.Monad.Trans.Except

Methods

liftCompare :: (a -> b -> Ordering) -> ExceptT e m a -> ExceptT e m b -> Ordering Source

(Read e, Read1 m) => Read1 (ExceptT e m)
Instance details

Defined in Control.Monad.Trans.Except

Methods

liftReadsPrec :: (Int -> ReadS a) -> ReadS [a] -> Int -> ReadS (ExceptT e m a) Source

liftReadList :: (Int -> ReadS a) -> ReadS [a] -> ReadS [ExceptT e m a] Source

liftReadPrec :: ReadPrec a -> ReadPrec [a] -> ReadPrec (ExceptT e m a) Source

liftReadListPrec :: ReadPrec a -> ReadPrec [a] -> ReadPrec [ExceptT e m a] Source

(Show e, Show1 m) => Show1 (ExceptT e m)
Instance details

Defined in Control.Monad.Trans.Except

Methods

liftShowsPrec :: (Int -> a -> ShowS) -> ([a] -> ShowS) -> Int -> ExceptT e m a -> ShowS Source

liftShowList :: (Int -> a -> ShowS) -> ([a] -> ShowS) -> [ExceptT e m a] -> ShowS Source

MonadZip m => MonadZip (ExceptT e m)
Instance details

Defined in Control.Monad.Trans.Except

Methods

mzip :: ExceptT e m a -> ExceptT e m b -> ExceptT e m (a, b) Source

mzipWith :: (a -> b -> c) -> ExceptT e m a -> ExceptT e m b -> ExceptT e m c Source

munzip :: ExceptT e m (a, b) -> (ExceptT e m a, ExceptT e m b) Source

MonadIO m => MonadIO (ExceptT e m)
Instance details

Defined in Control.Monad.Trans.Except

Methods

liftIO :: IO a -> ExceptT e m a Source

(Functor m, Monad m, Monoid e) => Alternative (ExceptT e m)
Instance details

Defined in Control.Monad.Trans.Except

Methods

empty :: ExceptT e m a Source

(<|>) :: ExceptT e m a -> ExceptT e m a -> ExceptT e m a Source

some :: ExceptT e m a -> ExceptT e m [a] Source

many :: ExceptT e m a -> ExceptT e m [a] Source

(Monad m, Monoid e) => MonadPlus (ExceptT e m)
Instance details

Defined in Control.Monad.Trans.Except

Methods

mzero :: ExceptT e m a Source

mplus :: ExceptT e m a -> ExceptT e m a -> ExceptT e m a Source

MonadCont m => MonadCont (ExceptT e m)

Since: mtl-2.2

Instance details

Defined in Control.Monad.Cont.Class

Methods

callCC :: ((a -> ExceptT e m b) -> ExceptT e m a) -> ExceptT e m a Source

(Eq e, Eq1 m, Eq a) => Eq (ExceptT e m a)
Instance details

Defined in Control.Monad.Trans.Except

Methods

(==) :: ExceptT e m a -> ExceptT e m a -> Bool

(/=) :: ExceptT e m a -> ExceptT e m a -> Bool

(Ord e, Ord1 m, Ord a) => Ord (ExceptT e m a)
Instance details

Defined in Control.Monad.Trans.Except

Methods

compare :: ExceptT e m a -> ExceptT e m a -> Ordering

(<) :: ExceptT e m a -> ExceptT e m a -> Bool

(<=) :: ExceptT e m a -> ExceptT e m a -> Bool

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

(>=) :: ExceptT e m a -> ExceptT e m a -> Bool

max :: ExceptT e m a -> ExceptT e m a -> ExceptT e m a

min :: ExceptT e m a -> ExceptT e m a -> ExceptT e m a

(Read e, Read1 m, Read a) => Read (ExceptT e m a)
Instance details

Defined in Control.Monad.Trans.Except

(Show e, Show1 m, Show a) => Show (ExceptT e m a)
Instance details

Defined in Control.Monad.Trans.Except

Methods

showsPrec :: Int -> ExceptT e m a -> ShowS Source

show :: ExceptT e m a -> String Source

showList :: [ExceptT e m a] -> ShowS Source

type Except e = ExceptT e Identity Source

The parameterizable exception monad.

Computations are either exceptions or normal values.

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.

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

The inverse of ExceptT.

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

Map the unwrapped computation using the given function.

withExceptT :: forall (m :: Type -> Type) e e' a. Functor m => (e -> e') -> ExceptT e m a -> ExceptT e' m a Source

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

runExcept :: Except e a -> Either e a Source

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

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

Map the unwrapped computation using the given function.

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

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

module Control.Monad

module Control.Monad.Fix

module Control.Monad.Trans

Example 1: Custom Error Data Type

Here is an example that demonstrates the use of a custom error data type with the throwError and catchError exception mechanism from MonadError. The example throws an exception if the user enters an empty string or a string longer than 5 characters. Otherwise it prints length of the string.

-- This is the type to represent length calculation error.
data LengthError = EmptyString  -- Entered string was empty.
          | StringTooLong Int   -- A string is longer than 5 characters.
                                -- Records a length of the string.
          | OtherError String   -- Other error, stores the problem description.

-- Converts LengthError to a readable message.
instance Show LengthError where
  show EmptyString = "The string was empty!"
  show (StringTooLong len) =
      "The length of the string (" ++ (show len) ++ ") is bigger than 5!"
  show (OtherError msg) = msg

-- For our monad type constructor, we use Either LengthError
-- which represents failure using Left LengthError
-- or a successful result of type a using Right a.
type LengthMonad = Either LengthError

main = do
  putStrLn "Please enter a string:"
  s <- getLine
  reportResult (calculateLength s)

-- Attempts to calculate length and throws an error if the provided string is
-- empty or longer than 5 characters.
-- (Throwing an error in this monad means returning a 'Left'.)
calculateLength :: String -> LengthMonad Int
calculateLength [] = throwError EmptyString
calculateLength s | len > 5 = throwError (StringTooLong len)
                  | otherwise = return len
  where len = length s

-- Prints result of the string length calculation.
reportResult :: LengthMonad Int -> IO ()
reportResult (Right len) = putStrLn ("The length of the string is " ++ (show len))
reportResult (Left e) = putStrLn ("Length calculation failed with error: " ++ (show e))

Example 2: Using ExceptT Monad Transformer

ExceptT monad transformer can be used to add error handling to another monad. Here is an example how to combine it with an IO monad:

import Control.Monad.Except

-- An IO monad which can return String failure.
-- It is convenient to define the monad type of the combined monad,
-- especially if we combine more monad transformers.
type LengthMonad = ExceptT String IO

main = do
  -- runExceptT removes the ExceptT wrapper
  r <- runExceptT calculateLength
  reportResult r

-- Asks user for a non-empty string and returns its length.
-- Throws an error if user enters an empty string.
calculateLength :: LengthMonad Int
calculateLength = do
  -- all the IO operations have to be lifted to the IO monad in the monad stack
  liftIO $ putStrLn "Please enter a non-empty string: "
  s <- liftIO getLine
  if null s
    then throwError "The string was empty!"
    else return $ length s

-- Prints result of the string length calculation.
reportResult :: Either String Int -> IO ()
reportResult (Right len) = putStrLn ("The length of the string is " ++ (show len))
reportResult (Left e) = putStrLn ("Length calculation failed with error: " ++ (show e))

© The University of Glasgow and others
Licensed under a BSD-style license (see top of the page).
https://downloads.haskell.org/~ghc/8.10.2/docs/html/libraries/mtl-2.2.2/Control-Monad-Except.html