monads-tf-0.3.0.1: Monad classes, using type families

Copyright | (c) Michael Weber <michael.weber@post.rwth-aachen.de> 2001 (c) Jeff Newbern 2003-2006 (c) Andriy Palamarchuk 2006
License | BSD-style (see the file LICENSE)
Maintainer | ross@soi.city.ac.uk
Stability | experimental
Portability | non-portable (type families)
Safe Haskell | None
Language | GHC2021

Control.Monad.Except

Contents

- Monads with error handling
- The ExceptT monad transformer
- Example 1: Custom Error Data Type
- Example 2: Using ExceptT Monad Transformer

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.
Zero and plus:
  Zero is represented by an empty error and the plus operation executes its second argument if the first fails.
Example type:
  ``Either` String a`

The Error monad (also called the Exception monad).

Synopsis

- class Monad m => MonadError (m :: Type -> Type) where
  - type ErrorType (m :: Type -> Type)
  - throwError :: ErrorType m -> m a
  - catchError :: m a -> (ErrorType m -> m a) -> m a
- tryError :: MonadError m => m a -> m (Either (ErrorType m) a)
- withError :: MonadError m => (ErrorType m -> ErrorType m) -> m a -> m a
- newtype ExceptT e (m :: Type -> Type) a = ExceptT (m (Either e a))
- runExceptT :: ExceptT e m a -> m (Either e a)
- mapExceptT :: (m (Either e a) -> n (Either e' b)) -> ExceptT e m a -> ExceptT e' n b
- withExceptT :: forall (m :: Type -> Type) e e' a. Functor m => (e -> e') -> ExceptT e m a -> ExceptT e' m a
- module Control.Monad
- module Control.Monad.Fix
- module Control.Monad.Trans

# Monads with error handling

class Monad m => MonadError (m :: Type -> Type) 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 a monad type constructor other than ``Either` e`, in which case you will have to explicitly define an instance of the `MonadError` class.

Associated Types

type ErrorType (m :: Type -> Type) Source #

Methods

throwError :: ErrorType m -> m a Source #

Is used within a monadic computation to begin exception processing.

catchError :: m a -> (ErrorType m -> 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 IO Source # |

Instance details

Defined in Control.Monad.Except.Class

Associated Types

type ErrorType IO |
Instance details Defined in Control.Monad.Except.Class type ErrorType IO = IOError

Methods

throwError :: ErrorType IO -> IO a Source #

catchError :: IO a -> (ErrorType IO -> IO a) -> IO a Source #

MonadError (Either e) Source # |

Instance details

Defined in Control.Monad.Except.Class

Associated Types

type ErrorType (Either e) |
Instance details Defined in Control.Monad.Except.Class type ErrorType (Either e) = e

Methods

throwError :: ErrorType (Either e) -> Either e a Source #

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

MonadError m => MonadError (MaybeT m) Source # |

Instance details

Defined in Control.Monad.Except.Class

Associated Types

type ErrorType (MaybeT m) |
Instance details Defined in Control.Monad.Except.Class type ErrorType (MaybeT m) = ErrorType m

Methods

throwError :: ErrorType (MaybeT m) -> MaybeT m a Source #

catchError :: MaybeT m a -> (ErrorType (MaybeT m) -> MaybeT m a) -> MaybeT m a Source #

Monad m => MonadError (ExceptT e m) Source # |

Instance details

Defined in Control.Monad.Except.Class

Associated Types

type ErrorType (ExceptT e m) |
Instance details Defined in Control.Monad.Except.Class type ErrorType (ExceptT e m) = e

Methods

throwError :: ErrorType (ExceptT e m) -> ExceptT e m a Source #

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

MonadError m => MonadError (IdentityT m) Source # |

Instance details

Defined in Control.Monad.Except.Class

Associated Types

type ErrorType (IdentityT m) |
Instance details Defined in Control.Monad.Except.Class type ErrorType (IdentityT m) = ErrorType m

Methods

throwError :: ErrorType (IdentityT m) -> IdentityT m a Source #

catchError :: IdentityT m a -> (ErrorType (IdentityT m) -> IdentityT m a) -> IdentityT m a Source #

MonadError m => MonadError (ReaderT r m) Source # |

Instance details

Defined in Control.Monad.Except.Class

Associated Types

type ErrorType (ReaderT r m) |
Instance details Defined in Control.Monad.Except.Class type ErrorType (ReaderT r m) = ErrorType m

Methods

throwError :: ErrorType (ReaderT r m) -> ReaderT r m a Source #

catchError :: ReaderT r m a -> (ErrorType (ReaderT r m) -> ReaderT r m a) -> ReaderT r m a Source #

MonadError m => MonadError (StateT s m) Source # |

Instance details

Defined in Control.Monad.Except.Class

Associated Types

type ErrorType (StateT s m) |
Instance details Defined in Control.Monad.Except.Class type ErrorType (StateT s m) = ErrorType m

Methods

throwError :: ErrorType (StateT s m) -> StateT s m a Source #

catchError :: StateT s m a -> (ErrorType (StateT s m) -> StateT s m a) -> StateT s m a Source #

MonadError m => MonadError (StateT s m) Source # |

Instance details

Defined in Control.Monad.Except.Class

Associated Types

type ErrorType (StateT s m) |
Instance details Defined in Control.Monad.Except.Class type ErrorType (StateT s m) = ErrorType m

Methods

throwError :: ErrorType (StateT s m) -> StateT s m a Source #

catchError :: StateT s m a -> (ErrorType (StateT s m) -> StateT s m a) -> StateT s m a Source #

(Monoid w, MonadError m) => MonadError (WriterT w m) Source # |

Instance details

Defined in Control.Monad.Except.Class

Associated Types

type ErrorType (WriterT w m) |
Instance details Defined in Control.Monad.Except.Class type ErrorType (WriterT w m) = ErrorType m

Methods

throwError :: ErrorType (WriterT w m) -> WriterT w m a Source #

catchError :: WriterT w m a -> (ErrorType (WriterT w m) -> WriterT w m a) -> WriterT w m a Source #

(Monoid w, MonadError m) => MonadError (WriterT w m) Source # |

Instance details

Defined in Control.Monad.Except.Class

Associated Types

type ErrorType (WriterT w m) |
Instance details Defined in Control.Monad.Except.Class type ErrorType (WriterT w m) = ErrorType m

Methods

throwError :: ErrorType (WriterT w m) -> WriterT w m a Source #

catchError :: WriterT w m a -> (ErrorType (WriterT w m) -> WriterT w m a) -> WriterT w m a Source #

(Monoid w, MonadError m) => MonadError (RWST r w s m) Source # |

Instance details

Defined in Control.Monad.Except.Class

Associated Types

type ErrorType (RWST r w s m) |
Instance details Defined in Control.Monad.Except.Class type ErrorType (RWST r w s m) = ErrorType m

Methods

throwError :: ErrorType (RWST r w s m) -> RWST r w s m a Source #

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

(Monoid w, MonadError m) => MonadError (RWST r w s m) Source # |

Instance details

Defined in Control.Monad.Except.Class

Associated Types

type ErrorType (RWST r w s m) |
Instance details Defined in Control.Monad.Except.Class type ErrorType (RWST r w s m) = ErrorType m

Methods

throwError :: ErrorType (RWST r w s m) -> RWST r w s m a Source #

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

tryError :: MonadError m => m a -> m (Either (ErrorType m) a) Source #

`MonadError` analogue to the `try` function.

withError :: MonadError m => (ErrorType m -> ErrorType m) -> m a -> m a Source #

`MonadError` analogue to the `withExceptT` function. Modify the value (but not the type) of an error. The type is fixed because of the `ErrorType` type family.

# 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

Functor m => Generic1 (ExceptT e m :: Type -> Type) |

Instance details

Defined in Control.Monad.Trans.Except

Associated Types

type Rep1 (ExceptT e m :: Type -> Type) |
Instance details Defined in Control.Monad.Trans.Except type Rep1 (ExceptT e m :: Type -> Type) = D1 ('MetaData "ExceptT" "Control.Monad.Trans.Except" "transformers-0.6.1.1-inplace" 'True) (C1 ('MetaCons "ExceptT" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (m :.: Rec1 (Either e))))

Methods

from1 :: ExceptT e m a -> Rep1 (ExceptT e m) a Source #

to1 :: Rep1 (ExceptT e m) a -> 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 #
MonadIO m => MonadIO (ExceptT e m) |
Instance details Defined in Control.Monad.Trans.Except Methods liftIO :: IO a -> ExceptT e m a 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 #
(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 #
Contravariant m => Contravariant (ExceptT e m) |
Instance details Defined in Control.Monad.Trans.Except Methods contramap :: (a' -> a) -> ExceptT e m a -> ExceptT e m a' Source # (>$) :: b -> ExceptT e m b -> 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 #
(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 #
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 #
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 #
(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 #
MonadFail m => MonadFail (ExceptT e m) |
Instance details Defined in Control.Monad.Trans.Except Methods fail :: String -> 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 #
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 #
MonadCont m => MonadCont (ExceptT e m) Source # |
Instance details Defined in Control.Monad.Cont.Class Methods callCC :: ((a -> ExceptT e m b) -> ExceptT e m a) -> ExceptT e m a Source #
Monad m => MonadError (ExceptT e m) Source # |

Instance details

Defined in Control.Monad.Except.Class

Associated Types

type ErrorType (ExceptT e m) |
Instance details Defined in Control.Monad.Except.Class type ErrorType (ExceptT e m) = e

Methods

throwError :: ErrorType (ExceptT e m) -> ExceptT e m a Source #

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

MonadRWS m => MonadRWS (ExceptT e m) Source # |
Instance details Defined in Control.Monad.RWS.Class
MonadReader m => MonadReader (ExceptT e m) Source # |

Instance details

Defined in Control.Monad.Reader.Class

Associated Types

type EnvType (ExceptT e m) |
Instance details Defined in Control.Monad.Reader.Class type EnvType (ExceptT e m) = EnvType m

Methods

ask :: ExceptT e m (EnvType (ExceptT e m)) Source #

local :: (EnvType (ExceptT e m) -> EnvType (ExceptT e m)) -> ExceptT e m a -> ExceptT e m a Source #

MonadState m => MonadState (ExceptT e m) Source # |

Instance details

Defined in Control.Monad.State.Class

Associated Types

type StateType (ExceptT e m) |
Instance details Defined in Control.Monad.State.Class type StateType (ExceptT e m) = StateType m

Methods

get :: ExceptT e m (StateType (ExceptT e m)) Source #

put :: StateType (ExceptT e m) -> ExceptT e m () Source #

MonadWriter m => MonadWriter (ExceptT e m) Source # |

Instance details

Defined in Control.Monad.Writer.Class

Associated Types

type WriterType (ExceptT e m) |
Instance details Defined in Control.Monad.Writer.Class type WriterType (ExceptT e m) = WriterType m

Methods

tell :: WriterType (ExceptT e m) -> ExceptT e m () Source #

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

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

Generic (ExceptT e m a) |

Instance details

Defined in Control.Monad.Trans.Except

Associated Types

type Rep (ExceptT e m a) |
Instance details Defined in Control.Monad.Trans.Except type Rep (ExceptT e m a) = D1 ('MetaData "ExceptT" "Control.Monad.Trans.Except" "transformers-0.6.1.1-inplace" 'True) (C1 ('MetaCons "ExceptT" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 (m (Either e a)))))

Methods

from :: ExceptT e m a -> Rep (ExceptT e m a) x Source #

to :: Rep (ExceptT e m a) x -> ExceptT e m a Source #

(Read e, Read1 m, Read a) => Read (ExceptT e m a) |
Instance details Defined in Control.Monad.Trans.Except Methods readsPrec :: Int -> ReadS (ExceptT e m a) Source # readList :: ReadS [ExceptT e m a] Source # readPrec :: ReadPrec (ExceptT e m a) Source # readListPrec :: ReadPrec [ExceptT e m a] Source #
(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 #
(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 Source # (/=) :: ExceptT e m a -> ExceptT e m a -> Bool Source #
(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 Source #

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

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

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

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

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

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

type Rep1 (ExceptT e m :: Type -> Type) |
Instance details Defined in Control.Monad.Trans.Except type Rep1 (ExceptT e m :: Type -> Type) = D1 ('MetaData "ExceptT" "Control.Monad.Trans.Except" "transformers-0.6.1.1-inplace" 'True) (C1 ('MetaCons "ExceptT" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (m :.: Rec1 (Either e))))
type ErrorType (ExceptT e m) Source # |
Instance details Defined in Control.Monad.Except.Class type ErrorType (ExceptT e m) = e
type EnvType (ExceptT e m) Source # |
Instance details Defined in Control.Monad.Reader.Class type EnvType (ExceptT e m) = EnvType m
type StateType (ExceptT e m) Source # |
Instance details Defined in Control.Monad.State.Class type StateType (ExceptT e m) = StateType m
type WriterType (ExceptT e m) Source # |
Instance details Defined in Control.Monad.Writer.Class type WriterType (ExceptT e m) = WriterType m
type Rep (ExceptT e m a) |
Instance details Defined in Control.Monad.Trans.Except type Rep (ExceptT e m a) = D1 ('MetaData "ExceptT" "Control.Monad.Trans.Except" "transformers-0.6.1.1-inplace" 'True) (C1 ('MetaCons "ExceptT" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 (m (Either e a)))))

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.

-     runExceptT (mapExceptT f m) = f (runExceptT m)

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.

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.

    import Control.Monad.Except

    -- 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 (calculateLengthOrFail s)

    -- Attempts to calculate length and throws an error if the provided string is
    -- empty or longer than 5 characters.
    -- The processing is done in Either monad.
    calculateLengthOrFail :: String -> LengthMonad Int
    calculateLengthOrFail [] = throwError EmptyString
    calculateLengthOrFail 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

``ErrorT`` 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))