How to handle this simple IO exception in Haskell

McBear Holden

Deal all:

In the below code, getDirectoryContents dir may fail. For example the dir might not exists. How to catch this and throw a meaningful message to the user? I know that IO exception handling has been asked many times but i still can't find a simple way to do this.

walk :: FilePath -> IO()
walk dir = do
    contentsFullPath <- getDirectoryContents dir >>= removeDotFile >>= getFullPath
    dirList <- filterM doesDirectoryExist contentsFullPath
    fileList <- filterM doesFileExist contentsFullPath
    forM_ fileList processFile >> forM_ dirList walk            
    where
        removeDotFile = return . filter (`notElem` ["..", "."])
        getFullPath = return . zipWith ( </> ) (repeat dir)
        processFile = getFileSize
Daniel Gratzer

You're looking for the module Control.Exception with it's host of functions for catching/handling exceptions,

in your case

handler :: IOException -> IO [FilePath]
handler = undefined

walk :: FilePath -> IO()
walk dir = do
    contentsFullPath <- handle handler $ 
                          getDirectoryContents dir
                          >>= removeDotFile
                          >>= getFullPath
    ...

handle is just the same as catch except the arguments are swapped. It takes an IO computation which might throw an exception, a handler for some type of exception, and runs the computation, catching that particular type of exception.

Since you might not be able to return an appropriate list of FilePaths, you may want to catch the exception higher up, with something more like walk dir = handle handler $ do ... and then you can simply have a handler of type IOException -> IO ().

Since in this case we're interested in IO exceptions, that what we use.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Haskell IO - hGetContents: illegal operation (handle is closed)

From Dev

How to handle this simple IO exception in Haskell

From Dev

How do I handle this exception

From Dev

How to "break" IO action in haskell

From Dev

How to handle generic deserialisation in haskell?

From Dev

How to handle thrown exception in NUnit

From Dev

How to handle or avoid BlockedIndefinitelyOnSTM exception?

From Dev

How to handle Watir::Exception::UnknownObjectException:

From Dev

How to handle lots of constants in Haskell?

From Dev

How to handle Exception in Java JDBC?

From Dev

How to handle runtime errors in Haskell?

From Dev

How to use catch with IO Handle

From Dev

How to handle Token Mismatch Exception

From Dev

How to ignore exception or Error and continue IO actions in Haskell?

From Dev

How to handle exception in the "finally" block?

From Dev

How to handle an exception occurring in a filter?

From Dev

How to handle exception and exit?

From Dev

How to handle exception outside of page?

From Dev

Haskell IO - hGetContents: illegal operation (handle is closed)

From Dev

Haskell: exception handling in non-IO monads

From Dev

How to handle an error with createProcess in Haskell?

From Dev

How to handle or avoid BlockedIndefinitelyOnSTM exception?

From Dev

How to handle exception in java

From Dev

multiline contents of a IO handle in haskell display nothing

From Dev

How handle exception from ContentProvider?

From Dev

How to handle a consumer exception as an ack?

From Dev

How to Handle HttpAsyncClient exception

From Dev

Can not handle properly a simple exception

From Dev

How to handle HtmlDocument null exception

Related Related

HotTag

Archive