osager February 2016

How to ignore exception or Error and continue IO actions in Haskell?

main :: IO ()
main = do
    res <- try (mapM_ (callProcess "pandoc") [["--help"],["-eoc"],["-exois"],["--help"]]) :: IO (Either SomeException ())
    case res of
        Right a -> print "done"
        Left e -> print "error"

in the above code, some of the argument for the command pandoc are wrong and pandoc will throw errors. but I want my code to continue without stopping there and finish the mapM_

What Exception handling should I use?

The error I have is:

    pandoc: unrecognized option `-e'
Try pandoc --help for more information.
"error"

Answers


Daniel Wagner February 2016

Instead of trying your best to do a whole block of commands, have a block of commands that each try their best. Change try (mapM_ foo bar) into mapM_ (try . foo) bar.

Post Status

Asked in February 2016
Viewed 1,664 times
Voted 12
Answered 1 times

Search




Leave an answer