<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>Control.Monad.Error</title><link href="ocean.css" rel="stylesheet" type="text/css" title="Ocean" /><script src="haddock-util.js" type="text/javascript"></script><script type="text/javascript">//<![CDATA[ window.onload = function () {pageLoad();setSynopsis("mini_Control-Monad-Error.html");}; //]]> </script></head><body><div id="package-header"><ul class="links" id="page-menu"><li><a href="src/Control-Monad-Error.html">Source</a></li><li><a href="index.html">Contents</a></li><li><a href="doc-index.html">Index</a></li></ul><p class="caption">mtl-2.0.1.0: Monad classes, using functional dependencies</p></div><div id="content"><div id="module-header"><table class="info"><tr><th>Portability</th><td>non-portable (multi-parameter type classes)</td></tr><tr><th>Stability</th><td>experimental</td></tr><tr><th>Maintainer</th><td>libraries@haskell.org</td></tr></table><p class="caption">Control.Monad.Error</p></div><div id="table-of-contents"><p class="caption">Contents</p><ul><li><a href="#g:1">Monads with error handling </a></li><li><a href="#g:2">The ErrorT monad transformer </a></li><li><a href="#g:3">Example 1: Custom Error Data Type </a></li><li><a href="#g:4">Example 2: Using ErrorT Monad Transformer </a></li></ul></div><div id="description"><p class="caption">Description</p><div class="doc"><dl><dt>Computation type:</dt><dd> Computations which may fail or throw exceptions. </dd><dt>Binding strategy:</dt><dd> 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. </dd><dt>Useful for:</dt><dd> Building computations from sequences of functions that may fail or using exception handling to structure error handling. </dd><dt>Zero and plus:</dt><dd> Zero is represented by an empty error and the plus operation executes its second argument if the first fails. </dd><dt>Example type:</dt><dd> <code><code>Data.Either</code> String a</code> </dd></dl><p>The Error monad (also called the Exception monad). </p></div></div><div id="synopsis"><p id="control.syn" class="caption expander" onclick="toggleSection('syn')">Synopsis</p><ul id="section.syn" class="hide" onclick="toggleSection('syn')"><li class="src short"><span class="keyword">class</span> <a href="/usr/share/doc/ghc/html/libraries/base-4.3.1.0/Control-Monad.html#t:Monad">Monad</a> m => <a href="#t:MonadError">MonadError</a> e m | m -> e <span class="keyword">where</span><ul class="subs"><li><a href="#v:throwError">throwError</a> :: e -> m a</li><li><a href="#v:catchError">catchError</a> :: m a -> (e -> m a) -> m a</li></ul></li><li class="src short"><span class="keyword">class</span> <a href="#t:Error">Error</a> a <span class="keyword">where</span><ul class="subs"><li><a href="#v:noMsg">noMsg</a> :: a</li><li><a href="#v:strMsg">strMsg</a> :: <a href="/usr/share/doc/ghc/html/libraries/base-4.3.1.0/Data-Char.html#t:String">String</a> -> a</li></ul></li><li class="src short"><span class="keyword">newtype</span> <a href="#t:ErrorT">ErrorT</a> e m a = <a href="#v:ErrorT">ErrorT</a> {<ul class="subs"><li><a href="#v:runErrorT">runErrorT</a> :: m (<a href="/usr/share/doc/ghc/html/libraries/base-4.3.1.0/Data-Either.html#t:Either">Either</a> e a)</li></ul>}</li><li class="src short"><a href="#v:mapErrorT">mapErrorT</a> :: (m (<a href="/usr/share/doc/ghc/html/libraries/base-4.3.1.0/Data-Either.html#t:Either">Either</a> e a) -> n (<a href="/usr/share/doc/ghc/html/libraries/base-4.3.1.0/Data-Either.html#t:Either">Either</a> e' b)) -> <a href="Control-Monad-Error.html#t:ErrorT">ErrorT</a> e m a -> <a href="Control-Monad-Error.html#t:ErrorT">ErrorT</a> e' n b</li><li class="src short">module <a href="/usr/share/doc/ghc/html/libraries/base-4.3.1.0/Control-Monad.html">Control.Monad</a></li><li class="src short">module <a href="/usr/share/doc/ghc/html/libraries/base-4.3.1.0/Control-Monad-Fix.html">Control.Monad.Fix</a></li><li class="src short">module <a href="Control-Monad-Trans.html">Control.Monad.Trans</a></li></ul></div><div id="interface"><h1 id="g:1">Monads with error handling </h1><div class="top"><p class="src"><span class="keyword">class</span> <a href="/usr/share/doc/ghc/html/libraries/base-4.3.1.0/Control-Monad.html#t:Monad">Monad</a> m => <a name="t:MonadError" class="def">MonadError</a> e m | m -> e <span class="keyword">where</span><a href="src/Control-Monad-Error-Class.html#MonadError" class="link">Source</a></p><div class="doc"><p>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. </p><p>Is parameterized over the type of error information and the monad type constructor. It is common to use <code><code>Data.Either</code> String</code> 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 <code><a href="Control-Monad-Error.html#t:MonadError">MonadError</a></code> class. You can also define your own error type and/or use a monad type constructor other than <code><code><a href="/usr/share/doc/ghc/html/libraries/base-4.3.1.0/Data-Either.html#t:Either">Either</a></code> <code><a href="/usr/share/doc/ghc/html/libraries/base-4.3.1.0/Data-Char.html#t:String">String</a></code></code> or <code><code><a href="/usr/share/doc/ghc/html/libraries/base-4.3.1.0/Data-Either.html#t:Either">Either</a></code> <code><a href="/usr/share/doc/ghc/html/libraries/base-4.3.1.0/System-IO-Error.html#t:IOError">IOError</a></code></code>. In these cases you will have to explicitly define instances of the <code><a href="Control-Monad-Error.html#t:Error">Error</a></code> and/or <code><a href="Control-Monad-Error.html#t:MonadError">MonadError</a></code> classes. </p></div><div class="subs methods"><p class="caption">Methods</p><p class="src"><a name="v:throwError" class="def">throwError</a> :: e -> m a<a href="src/Control-Monad-Error-Class.html#throwError" class="link">Source</a></p><div class="doc"><p>Is used within a monadic computation to begin exception processing. </p></div><p class="src"><a name="v:catchError" class="def">catchError</a> :: m a -> (e -> m a) -> m a<a href="src/Control-Monad-Error-Class.html#catchError" class="link">Source</a></p><div class="doc"><p>A handler function to handle previous errors and return to normal execution. A common idiom is: </p><pre> do { action1; action2; action3 } `catchError` handler </pre><p>where the <code>action</code> functions can call <code><a href="Control-Monad-Error.html#v:throwError">throwError</a></code>. Note that <code>handler</code> and the do-block must have the same return type. </p></div></div><div class="subs instances"><p id="control.i:MonadError" class="caption collapser" onclick="toggleSection('i:MonadError')">Instances</p><div id="section.i:MonadError" class="show"><table><tr><td class="src"><a href="Control-Monad-Error.html#t:MonadError">MonadError</a> <a href="/usr/share/doc/ghc/html/libraries/base-4.3.1.0/Control-Exception-Base.html#t:IOException">IOException</a> <a href="/usr/share/doc/ghc/html/libraries/base-4.3.1.0/System-IO.html#t:IO">IO</a></td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Control-Monad-Error.html#t:MonadError">MonadError</a> e m => <a href="Control-Monad-Error.html#t:MonadError">MonadError</a> e (<a href="/usr/share/doc/ghc/html/libraries/transformers-0.2.2.0/Control-Monad-Trans-Maybe.html#t:MaybeT">MaybeT</a> m)</td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Control-Monad-Error.html#t:MonadError">MonadError</a> e m => <a href="Control-Monad-Error.html#t:MonadError">MonadError</a> e (<a href="Control-Monad-List.html#t:ListT">ListT</a> m)</td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Control-Monad-Error.html#t:MonadError">MonadError</a> e m => <a href="Control-Monad-Error.html#t:MonadError">MonadError</a> e (<a href="/usr/share/doc/ghc/html/libraries/transformers-0.2.2.0/Control-Monad-Trans-Identity.html#t:IdentityT">IdentityT</a> m)</td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Control-Monad-Error.html#t:Error">Error</a> e => <a href="Control-Monad-Error.html#t:MonadError">MonadError</a> e (<a href="/usr/share/doc/ghc/html/libraries/base-4.3.1.0/Data-Either.html#t:Either">Either</a> e)</td><td class="doc empty"> </td></tr><tr><td class="src">(<a href="/usr/share/doc/ghc/html/libraries/base-4.3.1.0/Data-Monoid.html#t:Monoid">Monoid</a> w, <a href="Control-Monad-Error.html#t:MonadError">MonadError</a> e m) => <a href="Control-Monad-Error.html#t:MonadError">MonadError</a> e (<a href="Control-Monad-Writer-Strict.html#t:WriterT">WriterT</a> w m)</td><td class="doc empty"> </td></tr><tr><td class="src">(<a href="/usr/share/doc/ghc/html/libraries/base-4.3.1.0/Data-Monoid.html#t:Monoid">Monoid</a> w, <a href="Control-Monad-Error.html#t:MonadError">MonadError</a> e m) => <a href="Control-Monad-Error.html#t:MonadError">MonadError</a> e (<a href="Control-Monad-Writer-Lazy.html#t:WriterT">WriterT</a> w m)</td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Control-Monad-Error.html#t:MonadError">MonadError</a> e m => <a href="Control-Monad-Error.html#t:MonadError">MonadError</a> e (<a href="Control-Monad-State-Strict.html#t:StateT">StateT</a> s m)</td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Control-Monad-Error.html#t:MonadError">MonadError</a> e m => <a href="Control-Monad-Error.html#t:MonadError">MonadError</a> e (<a href="Control-Monad-State-Lazy.html#t:StateT">StateT</a> s m)</td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Control-Monad-Error.html#t:MonadError">MonadError</a> e m => <a href="Control-Monad-Error.html#t:MonadError">MonadError</a> e (<a href="Control-Monad-Reader.html#t:ReaderT">ReaderT</a> r m)</td><td class="doc empty"> </td></tr><tr><td class="src">(<a href="/usr/share/doc/ghc/html/libraries/base-4.3.1.0/Control-Monad.html#t:Monad">Monad</a> m, <a href="Control-Monad-Error.html#t:Error">Error</a> e) => <a href="Control-Monad-Error.html#t:MonadError">MonadError</a> e (<a href="Control-Monad-Error.html#t:ErrorT">ErrorT</a> e m)</td><td class="doc empty"> </td></tr><tr><td class="src">(<a href="/usr/share/doc/ghc/html/libraries/base-4.3.1.0/Data-Monoid.html#t:Monoid">Monoid</a> w, <a href="Control-Monad-Error.html#t:MonadError">MonadError</a> e m) => <a href="Control-Monad-Error.html#t:MonadError">MonadError</a> e (<a href="Control-Monad-RWS-Strict.html#t:RWST">RWST</a> r w s m)</td><td class="doc empty"> </td></tr><tr><td class="src">(<a href="/usr/share/doc/ghc/html/libraries/base-4.3.1.0/Data-Monoid.html#t:Monoid">Monoid</a> w, <a href="Control-Monad-Error.html#t:MonadError">MonadError</a> e m) => <a href="Control-Monad-Error.html#t:MonadError">MonadError</a> e (<a href="Control-Monad-RWS-Lazy.html#t:RWST">RWST</a> r w s m)</td><td class="doc empty"> </td></tr></table></div></div></div><div class="top"><p class="src"><span class="keyword">class</span> <a name="t:Error" class="def">Error</a> a <span class="keyword">where</span></p><div class="doc"><p>An exception to be thrown. </p><p>Minimal complete definition: <code><a href="Control-Monad-Error.html#v:noMsg">noMsg</a></code> or <code><a href="Control-Monad-Error.html#v:strMsg">strMsg</a></code>. </p></div><div class="subs methods"><p class="caption">Methods</p><p class="src"><a name="v:noMsg" class="def">noMsg</a> :: a</p><div class="doc"><p>Creates an exception without a message. The default implementation is <code><code><a href="Control-Monad-Error.html#v:strMsg">strMsg</a></code> ""</code>. </p></div><p class="src"><a name="v:strMsg" class="def">strMsg</a> :: <a href="/usr/share/doc/ghc/html/libraries/base-4.3.1.0/Data-Char.html#t:String">String</a> -> a</p><div class="doc"><p>Creates an exception with a message. The default implementation of <code><code><a href="Control-Monad-Error.html#v:strMsg">strMsg</a></code> s</code> is <code><a href="Control-Monad-Error.html#v:noMsg">noMsg</a></code>. </p></div></div><div class="subs instances"><p id="control.i:Error" class="caption collapser" onclick="toggleSection('i:Error')">Instances</p><div id="section.i:Error" class="show"><table><tr><td class="src"><a href="Control-Monad-Error.html#t:Error">Error</a> <a href="/usr/share/doc/ghc/html/libraries/base-4.3.1.0/Control-Exception-Base.html#t:IOException">IOException</a></td><td class="doc empty"> </td></tr><tr><td class="src"><a href="/usr/share/doc/ghc/html/libraries/transformers-0.2.2.0/Control-Monad-Trans-Error.html#t:ErrorList">ErrorList</a> a => <a href="Control-Monad-Error.html#t:Error">Error</a> [a]</td><td class="doc"><p>A string can be thrown as an error. </p></td></tr></table></div></div></div><h1 id="g:2">The ErrorT monad transformer </h1><div class="top"><p class="src"><span class="keyword">newtype</span> <a name="t:ErrorT" class="def">ErrorT</a> e m a </p><div class="doc"><p>The error monad transformer. It can be used to add error handling to other monads. </p><p>The <code>ErrorT</code> Monad structure is parameterized over two things: </p><ul><li> e - The error type. </li><li> m - The inner monad. </li></ul><p>The <code><a href="/usr/share/doc/ghc/html/libraries/base-4.3.1.0/Control-Monad.html#v:return">return</a></code> function yields a successful computation, while <code>>>=</code> sequences two subcomputations, failing on the first error. </p></div><div class="subs constructors"><p class="caption">Constructors</p><table><tr><td class="src"><a name="v:ErrorT" class="def">ErrorT</a></td><td class="doc empty"> </td></tr><tr><td colspan="2"><div class="subs fields"><p class="caption">Fields</p><dl><dt class="src"><a name="v:runErrorT" class="def">runErrorT</a> :: m (<a href="/usr/share/doc/ghc/html/libraries/base-4.3.1.0/Data-Either.html#t:Either">Either</a> e a)</dt><dd class="doc empty"> </dd></dl><div class="clear"></div></div></td></tr></table></div><div class="subs instances"><p id="control.i:ErrorT" class="caption collapser" onclick="toggleSection('i:ErrorT')">Instances</p><div id="section.i:ErrorT" class="show"><table><tr><td class="src">(<a href="Control-Monad-Error.html#t:Error">Error</a> e, <a href="Control-Monad-RWS-Class.html#t:MonadRWS">MonadRWS</a> r w s m) => <a href="Control-Monad-RWS-Class.html#t:MonadRWS">MonadRWS</a> r w s (<a href="Control-Monad-Error.html#t:ErrorT">ErrorT</a> e m)</td><td class="doc empty"> </td></tr><tr><td class="src">(<a href="Control-Monad-Error.html#t:Error">Error</a> e, <a href="Control-Monad-Writer-Class.html#t:MonadWriter">MonadWriter</a> w m) => <a href="Control-Monad-Writer-Class.html#t:MonadWriter">MonadWriter</a> w (<a href="Control-Monad-Error.html#t:ErrorT">ErrorT</a> e m)</td><td class="doc empty"> </td></tr><tr><td class="src">(<a href="Control-Monad-Error.html#t:Error">Error</a> e, <a href="Control-Monad-State-Class.html#t:MonadState">MonadState</a> s m) => <a href="Control-Monad-State-Class.html#t:MonadState">MonadState</a> s (<a href="Control-Monad-Error.html#t:ErrorT">ErrorT</a> e m)</td><td class="doc empty"> </td></tr><tr><td class="src">(<a href="Control-Monad-Error.html#t:Error">Error</a> e, <a href="Control-Monad-Reader-Class.html#t:MonadReader">MonadReader</a> r m) => <a href="Control-Monad-Reader-Class.html#t:MonadReader">MonadReader</a> r (<a href="Control-Monad-Error.html#t:ErrorT">ErrorT</a> e m)</td><td class="doc empty"> </td></tr><tr><td class="src">(<a href="/usr/share/doc/ghc/html/libraries/base-4.3.1.0/Control-Monad.html#t:Monad">Monad</a> m, <a href="Control-Monad-Error.html#t:Error">Error</a> e) => <a href="Control-Monad-Error.html#t:MonadError">MonadError</a> e (<a href="Control-Monad-Error.html#t:ErrorT">ErrorT</a> e m)</td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Control-Monad-Error.html#t:Error">Error</a> e => <a href="/usr/share/doc/ghc/html/libraries/transformers-0.2.2.0/Control-Monad-Trans-Class.html#t:MonadTrans">MonadTrans</a> (<a href="Control-Monad-Error.html#t:ErrorT">ErrorT</a> e)</td><td class="doc empty"> </td></tr><tr><td class="src">(<a href="/usr/share/doc/ghc/html/libraries/base-4.3.1.0/Control-Monad.html#t:Monad">Monad</a> m, <a href="Control-Monad-Error.html#t:Error">Error</a> e) => <a href="/usr/share/doc/ghc/html/libraries/base-4.3.1.0/Control-Monad.html#t:Monad">Monad</a> (<a href="Control-Monad-Error.html#t:ErrorT">ErrorT</a> e m)</td><td class="doc empty"> </td></tr><tr><td class="src"><a href="/usr/share/doc/ghc/html/libraries/base-4.3.1.0/Control-Monad.html#t:Functor">Functor</a> m => <a href="/usr/share/doc/ghc/html/libraries/base-4.3.1.0/Control-Monad.html#t:Functor">Functor</a> (<a href="Control-Monad-Error.html#t:ErrorT">ErrorT</a> e m)</td><td class="doc empty"> </td></tr><tr><td class="src">(<a href="/usr/share/doc/ghc/html/libraries/base-4.3.1.0/Control-Monad-Fix.html#t:MonadFix">MonadFix</a> m, <a href="Control-Monad-Error.html#t:Error">Error</a> e) => <a href="/usr/share/doc/ghc/html/libraries/base-4.3.1.0/Control-Monad-Fix.html#t:MonadFix">MonadFix</a> (<a href="Control-Monad-Error.html#t:ErrorT">ErrorT</a> e m)</td><td class="doc empty"> </td></tr><tr><td class="src">(<a href="/usr/share/doc/ghc/html/libraries/base-4.3.1.0/Control-Monad.html#t:Monad">Monad</a> m, <a href="Control-Monad-Error.html#t:Error">Error</a> e) => <a href="/usr/share/doc/ghc/html/libraries/base-4.3.1.0/Control-Monad.html#t:MonadPlus">MonadPlus</a> (<a href="Control-Monad-Error.html#t:ErrorT">ErrorT</a> e m)</td><td class="doc empty"> </td></tr><tr><td class="src">(<a href="/usr/share/doc/ghc/html/libraries/base-4.3.1.0/Control-Monad.html#t:Functor">Functor</a> m, <a href="/usr/share/doc/ghc/html/libraries/base-4.3.1.0/Control-Monad.html#t:Monad">Monad</a> m) => <a href="/usr/share/doc/ghc/html/libraries/base-4.3.1.0/Control-Applicative.html#t:Applicative">Applicative</a> (<a href="Control-Monad-Error.html#t:ErrorT">ErrorT</a> e m)</td><td class="doc empty"> </td></tr><tr><td class="src">(<a href="/usr/share/doc/ghc/html/libraries/base-4.3.1.0/Control-Monad.html#t:Functor">Functor</a> m, <a href="/usr/share/doc/ghc/html/libraries/base-4.3.1.0/Control-Monad.html#t:Monad">Monad</a> m, <a href="Control-Monad-Error.html#t:Error">Error</a> e) => <a href="/usr/share/doc/ghc/html/libraries/base-4.3.1.0/Control-Applicative.html#t:Alternative">Alternative</a> (<a href="Control-Monad-Error.html#t:ErrorT">ErrorT</a> e m)</td><td class="doc empty"> </td></tr><tr><td class="src">(<a href="Control-Monad-Error.html#t:Error">Error</a> e, <a href="/usr/share/doc/ghc/html/libraries/transformers-0.2.2.0/Control-Monad-IO-Class.html#t:MonadIO">MonadIO</a> m) => <a href="/usr/share/doc/ghc/html/libraries/transformers-0.2.2.0/Control-Monad-IO-Class.html#t:MonadIO">MonadIO</a> (<a href="Control-Monad-Error.html#t:ErrorT">ErrorT</a> e m)</td><td class="doc empty"> </td></tr><tr><td class="src">(<a href="Control-Monad-Error.html#t:Error">Error</a> e, <a href="Control-Monad-Cont-Class.html#t:MonadCont">MonadCont</a> m) => <a href="Control-Monad-Cont-Class.html#t:MonadCont">MonadCont</a> (<a href="Control-Monad-Error.html#t:ErrorT">ErrorT</a> e m)</td><td class="doc empty"> </td></tr></table></div></div></div><div class="top"><p class="src"><a name="v:mapErrorT" class="def">mapErrorT</a> :: (m (<a href="/usr/share/doc/ghc/html/libraries/base-4.3.1.0/Data-Either.html#t:Either">Either</a> e a) -> n (<a href="/usr/share/doc/ghc/html/libraries/base-4.3.1.0/Data-Either.html#t:Either">Either</a> e' b)) -> <a href="Control-Monad-Error.html#t:ErrorT">ErrorT</a> e m a -> <a href="Control-Monad-Error.html#t:ErrorT">ErrorT</a> e' n b</p><div class="doc"><p>Map the unwrapped computation using the given function. </p><ul><li> <code><code><a href="Control-Monad-Error.html#v:runErrorT">runErrorT</a></code> (<code><a href="Control-Monad-Error.html#v:mapErrorT">mapErrorT</a></code> f m) = f (<code><a href="Control-Monad-Error.html#v:runErrorT">runErrorT</a></code> m</code>) </li></ul></div></div><div class="top"><p class="src">module <a href="/usr/share/doc/ghc/html/libraries/base-4.3.1.0/Control-Monad.html">Control.Monad</a></p></div><div class="top"><p class="src">module <a href="/usr/share/doc/ghc/html/libraries/base-4.3.1.0/Control-Monad-Fix.html">Control.Monad.Fix</a></p></div><div class="top"><p class="src">module <a href="Control-Monad-Trans.html">Control.Monad.Trans</a></p></div><h1 id="g:3">Example 1: Custom Error Data Type </h1><div class="doc"><p>Here is an example that demonstrates the use of a custom <code><a href="Control-Monad-Error.html#t:Error">Error</a></code> data type with the <code><a href="Control-Monad-Error.html#v:throwError">throwError</a></code> and <code><a href="Control-Monad-Error.html#v:catchError">catchError</a></code> exception mechanism from <code><a href="Control-Monad-Error.html#t:MonadError">MonadError</a></code>. 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. </p><pre>-- 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. -- We make LengthError an instance of the Error class -- to be able to throw it as an exception. instance Error LengthError where noMsg = OtherError "A String Error!" strMsg s = OtherError s -- 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) -- Wraps length calculation to catch the errors. -- Returns either length of the string or an error. calculateLength :: String -> LengthMonad Int calculateLength s = (calculateLengthOrFail s) `catchError` Left -- 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)) </pre></div><h1 id="g:4">Example 2: Using ErrorT Monad Transformer </h1><div class="doc"><p><code><code><a href="Control-Monad-Error.html#t:ErrorT">ErrorT</a></code></code> monad transformer can be used to add error handling to another monad. Here is an example how to combine it with an <code>IO</code> monad: </p><pre>import Control.Monad.Error -- 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 = ErrorT String IO main = do -- runErrorT removes the ErrorT wrapper r <- runErrorT 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)) </pre></div></div></div><div id="footer"><p>Produced by <a href="http://www.haskell.org/haddock/">Haddock</a> version 2.9.2</p></div></body></html>