Exception catch子句中的get变量

Exception catch子句中的get变量,exception,haskell,catch-block,stm,Exception,Haskell,Catch Block,Stm,你好,我需要写的功能比将发送和接收tcp和自动重新连接,如果连接断开的消息。消息取自STM通道 f ch a b = h <- connectTo a b forever $ do c <- atomically $ readTChan ch {- do smth with c -} `catch` (const $ f ch a b) 现在它对我有用了。 谢谢我想你的设计有误。尝试将tocatch子句分离到可能引发异常的唯

你好,我需要写的功能比将发送和接收tcp和自动重新连接,如果连接断开的消息。消息取自STM通道

f ch a b = 
    h <- connectTo a b
    forever $ do
        c <- atomically $ readTChan ch
        {- do smth with c -}
    `catch` (const $ f ch a b)
现在它对我有用了。
谢谢

我想你的设计有误。尝试将tocatch子句分离到可能引发异常的唯一操作。例如,如果读取c不会导致异常,为什么不将catch子句分开?

我认为您的设计有误。尝试将tocatch子句分离到可能引发异常的唯一操作。例如,如果读取
c
不会导致异常,为什么不将
catch
-子句分开?谢谢,这对我的情况有效。
chan
ch
参数的
fun
fun ch a b = do
h <- connectTo a b
forever $ do
    c <- atomically $ readTChan chan
    do   
        {- do smth with c -}
        `catch` (const $ do
            atomically $ unGetTChan chan c
            fun chan con
            )