
On 30 September 2010 16:46, C K Kashyap
Could you please review the change I've done to Don Stewart's scripting example -
run s = handle (fail . show) $ do (ih,oh,eh,pid) <- runInteractiveCommand s so <- hGetContents oh se <- hGetContents eh hClose ih ex <- waitForProcess pid case ex of ExitFailure e -> fail $ "Failed with status: " ++ show e _ | not (null se) -> fail se | otherwise -> return so
My change -
run s = handle (\e@(SomeException{}) -> return (show e)) $ do (ih,oh,eh,pid) <- runInteractiveCommand s so <- hGetContents oh se <- hGetContents eh hClose ih ex <- waitForProcess pid case ex of ExitFailure e -> fail $ "Failed with status: " ++ show e _ | not (null se) -> fail se | otherwise -> return so
The change looks good; note, however, that you're doing something subtly different if an error occurs: in the original, if an error occured then "fail" was used to forcibly terminate the program. Your variant however returns the shown error message. Depending on the situation, this is usually a bad thing (better off to use Maybe or Either). Also, here's the official rationale about why SomeException is not recommended: http://www.haskell.org/ghc/docs/6.12.2/html/libraries/base-4.2.0.1/Control-E... . My main summation of this is that it even catches when you try to use Ctrl-c to kill a program. -- Ivan Lazar Miljenovic Ivan.Miljenovic@gmail.com IvanMiljenovic.wordpress.com