Trying to handle a exception I found a related question talking about this:
what is the Frege equivalent to Haskell's "interact" function?
But it wasn't clear to me how to use the try/catch/finally
expressions.
The problem:
I wanted to read a file and return all its lines. In case it didn't exist I may wanted to return an empty list. Something like:
getContent :: String -> IO [String]
getContent filePath = openReader filePath >>= \reader -> reader.getLines
`catch` (\(e::FileNotFoundException) -> return [])
`finally` (println "something went wrong")
The previous code compiles but when executed it only shows the following:
frege> getContent "asdf"
java.io.FileNotFoundException: asdf (No such file or directory)
Questions:
- How should I change my code to act as expected (to return an empty list when the exception is raised) ?
- Is there any place in the docs related to this ? I'm sure more examples in the
docs/wiki/frege goodness
would help a lot.
Thanks