parent
21f1b20675
commit
d7741a3374
2 changed files with 26 additions and 0 deletions
@ -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…
Reference in new issue