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 awhere e is the exception and ais 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.