[Haskell-cafe] Problem with a sample from RWH
Ivan Lazar Miljenovic
ivan.miljenovic at gmail.com
Thu Sep 30 02:59:05 EDT 2010
On 30 September 2010 16:46, C K Kashyap <ckkashyap at gmail.com> wrote:
>
> 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-Exception.html#4
. 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 at gmail.com
IvanMiljenovic.wordpress.com
More information about the Haskell-Cafe
mailing list