Thursday, October 14, 2010

Haskell Exception Handling


import IO

foo :: Int -> IO()
foo n = do
x <- try readLn
case x of
Left ioerr -> if (n > 1)
then putStrLn "Not a number!" >> foo(n-1)
else putStrLn "Try too many times!"
Right num -> print (num + 12)

main = foo 3