O'Reilly logo

Haskell Cookbook by Yogesh Sajanikar

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

How it works...

  • The error function takes a string, and creates an exception. However, this exception is realizable only in the IO monad. Hence, we catch it while we are printing the result of div1.
  • The function catch runs an IO action and takes a handler to catch the exception. It is possible to catch all exceptions by specifying the type of exception as SomeException
  • The try function also runs an IO action. However, it returns the result in Either e a where e is the exception and a is the result of the IO action. Thus, we can check using Either that we have a valid result. 
  • Typically, IO functions such as readFile raise an exception of type IOException
  • We can create custom exceptions by creating an instance of Show and Exception. The ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required