How to catch (and ignore) a call to the error function?

后端 未结 2 1141
囚心锁ツ
囚心锁ツ 2021-02-18 18:30

I\'m surprised I couldn\'t find an answer to this anywhere.

I\'m writing a roguelike and I\'m using the ncurses library from hackage, which is a pretty good wrapper arou

2条回答
  •  遥遥无期
    2021-02-18 18:41

    You can do this using catch from Control.Exception. Note, however, that you need to be in the IO monad to do this.

    import qualified Control.Exception as Exc
    
    divide :: Float -> Float -> Float
    divide x 0 = error "Division by 0."
    divide x y = x / y
    
    main :: IO ()
    main = Exc.catch (print $ divide 5 0) handler
        where
            handler :: Exc.ErrorCall -> IO ()
            handler _ = putStrLn $ "You divided by 0!"
    

提交回复
热议问题