On Fri, Oct 12, 2012 at 12:49 PM, Dmitry Vyal <span dir="ltr">&lt;<a href="mailto:akamaus@gmail.com" target="_blank">akamaus@gmail.com</a>&gt;</span> wrote:<br><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Greetings,<br>
<br>
I found The MonadCatchIO-mtl package while looking for a way to catch exceptions in my custom StateT s (ReaderT r IO) monad. CatchIO worked flawlessly when handlers used the environment r from Reader, but things broken when I tried to use state. According to source, the handler is run with the same state which was passed to runStateT.<br>

<br>
instance MonadCatchIO m =&gt; MonadCatchIO (StateT s m) where<br>
    m `catch` f = StateT $ \s -&gt; (runStateT m s)<br>
                                   `Control.Monad.CatchIO.catch` (\e -&gt; runStateT (f e) s)<br>
<br>
And my finalizing action depends on the current state. Can you give an advice how to get the behavior I need?<br></blockquote><div><br></div><div>Catching an exception in IO is a non-local transfer of control from one part of the program to another, and the only thing that is propagated is the exception value itself. In general, given that the exceptional value could be introduced to your thread from outside the execution context (i.e. using &quot;killThread&quot;), it&#39;s not possible to save the updated state value. If you wanted those semantics, you would need to use a side-effecting reference like IORef.</div>
</div><div><br></div><div>G</div>-- <br>Gregory Collins &lt;<a href="mailto:greg@gregorycollins.net" target="_blank">greg@gregorycollins.net</a>&gt;<br>