[Haskell-cafe] Using CatchIO on StateT

Dmitry Vyal akamaus at gmail.com
Fri Oct 12 12:49:27 CEST 2012


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.

instance MonadCatchIO m => MonadCatchIO (StateT s m) where
     m `catch` f = StateT $ \s -> (runStateT m s)
                                    `Control.Monad.CatchIO.catch` (\e -> 
runStateT (f e) s)

And my finalizing action depends on the current state. Can you give an 
advice how to get the behavior I need?

Best regards,
Dmitry Vyal

More information about the Haskell-Cafe mailing list