[Haskell-cafe] Problem about exception.

Brandon S Allbery KF8NH allbery.b at gmail.com
Thu Jan 27 03:48:56 CET 2011

Hash: SHA1

On 1/26/11 21:10 , Magicloud Magiclouds wrote:
> Hi,
>   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
>   sendHello
>   readMsg >>= expect hello processHelloReply
>   sendWhatsyourname
>   readMsg >>= expect name processNameReply
>   And expect is something like:
> expect c f (cmd, msg) =
>   if c == cmd then
>     f msg
>   else
>     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
>   connect
>   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.

It looks to me like the very example in the Control.Exception documentation
will do this, with some renaming.

> -- many languages call this a "control exception"; think break/next
> -- etc.  It's an exception used internally to modify control flow.
> data ControlException = CEDebug | CEDisconnect
>      deriving (Show, Typeable)
> instance Exception ControlException

So now you can trap your ControlException above, or anything else is
presumably a true exception.

- -- 
brandon s. allbery     [linux,solaris,freebsd,perl]      allbery at kf8nh.com
system administrator  [openafs,heimdal,too many hats]  allbery at ece.cmu.edu
electrical and computer engineering, carnegie mellon university      KF8NH
Version: GnuPG v1.4.11 (Darwin)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/


More information about the Haskell-Cafe mailing list