Created
August 14, 2018 21:10
-
-
Save huytd/4f9d21cef67a07203b1281f975ed7cbd to your computer and use it in GitHub Desktop.
Error handling and IO in Haskell
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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