Boris
Boris

Reputation: 5176

Deciding which Exception to handle

In the context of deciding which Exception instance to handle for GHC versions >= 6.10, and given documentation for getDirectoryContents, can it be assumed that getDirectoryContents will only throw IOExceptions? E.g. no exceptions will get past

let handler = (const $ return []) :: IOException -> IO [FilePath]
contents <- handle handler $ getDirectoryContents dir

... well, apart from stack overflows and so on, but no exception thrown specifically by getDirectoryContents? It seems to me, that this should be the case, but I don't see it explicated in the documentation, so I just want to make sure.

Upvotes: 0

Views: 132

Answers (1)

chrisdb
chrisdb

Reputation: 1115

Well, I've never tried to read the code for System.Directory before, but looking at the code suggests that that's true. The only code related to error handling in the function definition is a call to modifyIOError which seems to add specific details like the function name and directory path to any IO error which is thrown. I had a quick look at the System.Posix.Directory module as well, and there are no obvious signs of errors other than IO errors being thrown there.

I just had a quick look though, so it's possible I missed something...

On the other hand, do you really care what kind of error it is at all? You appear to be trying to say "If something goes wrong, just return an empty list and carry on regardless". If that's all you want, why don't you just catch any error that gets thrown, instead of IO errors specifically?

What I mean to say is that the primary purpose of your error handler seems to be just to ignore errors, so why not just ignore them in general?

Upvotes: 1

Related Questions