[Haskell-cafe] Problem about exception.

Magicloud Magiclouds magicloud.magiclouds at gmail.com
Thu Jan 27 03:10:57 CET 2011

  Consider such a case: I'm making a client program. There is a
process, client and server exchange some information in a strict
order. So I do (persudo code):
exchange = do
  readMsg >>= expect hello processHelloReply
  readMsg >>= expect name processNameReply

  And expect is something like:
expect c f (cmd, msg) =
  if c == cmd then
    f msg
    fail "unexpected"

  This is OK until this situation: The server may send some special
command, for example DEBUG, DISCONNECT. This breaks the process above.
So I think I could throw the unexpected command to outer function to
handle. Something like:
main = do
  catch exchange $ \e -> do
    case e of
      UnexpectedCMD DEBUG -> -- ignore process
      UnexpectedCMD DISCONNECT -> -- disconnect process
      _ -> -- something really wrong

  Well, with Control.Exception, I do not know how to make this done.
And with something like Data.Either, I got more problem like:
36|    Couldn't match expected type `Control.Monad.Trans.State.Lazy.StateT
37|                                    InternalState IO b'
38|                with actual type `Either (Int8, ByteString) b1'
39|    Expected type: (Int8, ByteString)
40|                   -> Control.Monad.Trans.State.Lazy.StateT
InternalState IO b
41|      Actual type: (Int8, ByteString) -> Either (Int8, ByteString) b1

More information about the Haskell-Cafe mailing list