Complete chapter 30

master
Gaël Depreeuw 6 years ago
parent 21f1b20675
commit d7741a3374
  1. 5
      30-when-things-go-wrong/30-when-things-go-wrong.md
  2. 21
      30-when-things-go-wrong/src/StoppingTheParty.hs

@ -0,0 +1,5 @@
# 30 When things go wrong
## 30.5 The unbearable imprecision of trying
see [src/StoppingTheParty.hs](./src/StoppingTheParty.hs)

@ -0,0 +1,21 @@
module StoppingTheParty where
import Control.Concurrent(threadDelay)
import Control.Exception
import Control.Monad (forever)
import System.Random (randomRIO)
randomException :: IO ()
randomException = do
i <- randomRIO (1, 10 :: Int)
if i `elem` [1..9]
then throwIO DivideByZero
else throwIO StackOverflow
main :: IO ()
main = forever $ do
let tryS :: IO () -> IO (Either SomeException ())
tryS = try
_ <- tryS randomException
putStrLn "Live to loop another day!"
threadDelay (1 * 1000000)
Loading…
Cancel
Save