7stud
7stud

Reputation: 48599

How can I use catch properly?

When I use catch, I get this warning:

Warning: In the use of `catch'
         (imported from System.IO.Error):
         Deprecated: "Please use the new exceptions variant, Control.Exception.catch"

I've tried to use Control.Exception.catch instead, but I've been chasing errors down too many rat holes. Can someone post an example where the 2nd arg of catch catches the UnsupportedOperation error thrown by getTemporaryDirectory?

tempdir <- catch (getTemporaryDirectory) 
                 (\e -> return ".")  

Also, how do I use the catch syntax in the catch docs:

catch f (\e -> ... (e :: SomeException) ...)

I've tried every variation of that syntax I can think of, and I always get an error; it does not seem possible to enclose a type annotation(?) in parentheses like that in a lambda. Extremely poor documentation in my opinion. Unfortunately, that seems to be the standard that doc writers aspire to.

Upvotes: 3

Views: 300

Answers (1)

Gabriella Gonzalez
Gabriella Gonzalez

Reputation: 35089

A simple trick that works without any extensions is:

m `catch` (\e -> do
    let _ = e :: IOException  -- or whatever exception type you want to catch
    doStuff )

If you use the value e, then you can also just put the type annotation wherever you use it:

m `catch` (\e -> do
    print (e :: IOException)
    doStuff )

Upvotes: 3

Related Questions