[Haskell-cafe] Problem about exception.

Magicloud Magiclouds 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:
>> 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
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.11 (Darwin)
> Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/
>
> iEYEARECAAYFAk1A3RgACgkQIn7hlCsL25XRMgCeNEImC8VWPiM0fHB5Bu2ooFc8
> nz8An0TwHXXUxJl7bhndSVf2vxWbXpGf
> =HIqR
> -----END PGP SIGNATURE-----
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>

This is one way. But so the outer function could not know what
happened in "really wrong" situation.

-- 
竹密岂妨流水过
山高哪阻野云飞



More information about the Haskell-Cafe mailing list