[Haskell-beginners] error handling approaches

Michael Litchard michael at schmong.org
Mon Sep 12 03:02:33 CEST 2011


one idea I had was to make a [preProcess,initiateJob,makeChart]
Then I would map function jobProcessor over this list. If one of these
functions received a failure from the program they called, I could
generate an execption, and map would stop mapping over the list.
Is this a sound approach?

On Sun, Sep 11, 2011 at 5:08 PM, Michael Litchard <michael at schmong.org> wrote:
> I bring this to the mailing list because the only idea I can think of
> is a cascading case statement. That can't be the best option.
>
> On Sun, Sep 11, 2011 at 4:48 PM, Michael Litchard <michael at schmong.org> wrote:
>> 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