Ambiguous type variable arising from the use of `handle`

Viewed 921

In this code:

import System.Posix.Files
import Control.Exception

safeStat :: FilePath -> IO (Maybe FileStatus)
safeStat path =
  handle (\_ -> return Nothing) (getFileStatus path >>= (return . Just))

I'm getting this error (in ghci):

Ambiguous type variable `e0' in the constraint:
  (Exception e0) arising from a use of `handle'
...

I can get rid of the error by doding something like:

nothing :: IOException -> Maybe a
nothing _ = Nothing

safeStat :: FilePath -> IO (Maybe FileStatus)
safeStat path =
  handle (return . nothing) (getFileStatus path >>= (return . Just))

What's going on??? I'd like the hander to handle any exception.

2 Answers
Related