Skip to content

Instantly share code, notes, and snippets.

@huytd
Created August 14, 2018 21:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save huytd/4f9d21cef67a07203b1281f975ed7cbd to your computer and use it in GitHub Desktop.
Save huytd/4f9d21cef67a07203b1281f975ed7cbd to your computer and use it in GitHub Desktop.
Error handling and IO in Haskell
import Control.Exception
getWord :: IO String
getWord =
putStrLn "Enter something:" >>
getLine >>= \name ->
case name of
"bad" -> ioError $ userError "bad word detected"
_ -> return name
getWordWithoutBadWord :: IO String
getWordWithoutBadWord = catch getWord handler
where handler :: IOError -> IO String
handler e = return $ "(filtered) - reason: " ++ show e
main :: IO ()
main = getWordWithoutBadWord >>= \name -> putStrLn name
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment