Error Handling
Andrew J Bromage
ajb@spamcop.net
Mon, 9 Dec 2002 12:03:03 +1100
G'day all.
On Mon, Dec 09, 2002 at 11:35:54AM +1100, Thomas L. Bevan wrote:
> main = do (a:b:cs) <- getArgs
> i <- return (read a :: Int)
> j <- return (read b :: Int)
> putStr $ i + j
>
> How can I catch any possible cast exception?
How about this?
readM :: (Read a, Monad m) => String -> m a
readM s
= case readEither s of
Left err -> fail err
Right x -> return x
main = runErrorT main'
main' = do (a:b:cs) <- liftIO getArgs
(i::Int) <- readM a
(j::Int) <- readM b
liftIO (putStr $ i + j)
Cheers,
Andrew Bromage