[Haskell-cafe] Tricky exception handling

Bulat Ziganshin bulatz at HotPOP.com
Mon Dec 12 06:29:47 EST 2005

Hello Joel,

your code is unreadable, inefficent and don't handle async exceptions
between calls to `handle`. as i already sayed you'd better to use:

handle (...)
    do cmd <- read h ssl
       post $! Cmd $! cmd

Monday, December 12, 2005, 12:30:27 PM, you wrote:

JR> Folks,

JR> I have a thread that launches socket reader and writer threads. My  
JR> intent is to notify the main thread when an exception is caught by  
JR> the reader/writer. Is this code proper?

JR> ---
JR> withException :: (Event a -> IO ())
JR>                   -> IO ()
JR>                   -> IO Bool
JR> withException post action =
JR>      handle (\e -> do case e of
JR>                         AsyncException ThreadKilled -> return ()
JR>                         _ -> post $ NetworkError e
JR>                       return False
JR>             ) $ do { action; return True }

JR> writeLoop :: (Event a -> IO ()) -> Handle -> (SSL, BIO, BIO) -> IO ()
JR> writeLoop post h ssl =
JR>      do go <- withException post $
JR>               do cmd <- read h ssl
JR>                  post $! Cmd $! cmd
JR>         when go $ writeLoop post h ssl
JR> ---

JR>         Thanks, Joel

JR> --
JR> http://wagerlabs.com/

JR> _______________________________________________
JR> Haskell-Cafe mailing list
JR> Haskell-Cafe at haskell.org
JR> http://www.haskell.org/mailman/listinfo/haskell-cafe

Best regards,
 Bulat                            mailto:bulatz at HotPOP.com

More information about the Haskell-Cafe mailing list