[Haskell-beginners] error handling approaches

Michael Litchard michael at schmong.org
Mon Sep 12 01:48:49 CEST 2011

Here's the gist of the code I am looking to incorporate error handling into

> worldHandler :: ProcessState -> JobCount -> IO ()
> worldHandler world jCount = do
>    putStrLn "Entered worldHandler"
>    jcount <- takeMVar jCount
>    if jcount > 0
>       then incrementThenDone jcount
>       else doJobProcessing jcount
>       where incrementThenDone jcount = do
>                 putMVar jCount (jcount+1)

>             doJobProcessing jcount = do
>                 putMVar jCount (jcount+1)
>                 preProcess world
>                 initiateJob world
>                 makeChart world

Here's main

> main :: IO ()
> main = do
>     world <- (newEmptyMVar :: IO ProcessState)
>     jCount <- (newMVar 0 :: IO JobCount)
>     installHandler userDefinedSignal1 (Catch $ worldHandler world jCount) Nothing
>     forever (threadDelay 10000)

the functions preProcess, initiateJob, and makeChart is where I need
be concerned with errors. The idea is that if any of those functions
fail, I decrement jCount. call logError, and continue waiting for the
next signal.
each of these functions will be starting another process,
readProcessWithExitCode looks like what I want. I will then use the
exit code to determine success or failure.

General ideas about how to approach this, and any questions that come
to mind would be appreciated.

More information about the Beginners mailing list