[Haskell-cafe] Problem about exception.
magicloud.magiclouds at gmail.com
Thu Jan 27 04:00:28 CET 2011
On Thu, Jan 27, 2011 at 10:48 AM, Brandon S Allbery KF8NH
<allbery.b at gmail.com> wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
> On 1/26/11 21:10 , Magicloud Magiclouds wrote:
>> 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.
> 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
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.11 (Darwin)
> Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/
> -----END PGP SIGNATURE-----
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
This is one way. But so the outer function could not know what
happened in "really wrong" situation.
More information about the Haskell-Cafe