[Haskell-cafe] Problem on exception and laziness

Magicloud Magiclouds magicloud.magiclouds at gmail.com
Tue Apr 23 06:00:22 UTC 2019


Hi,
I am aware that laziness sometimes may cause exception handling in
unexpected ways. Recently I have such a problem that I cannot figure
out how to fix (simply).

I have a function `watch` that parse the content of /proc/. And I use
`catch` to wrap `readFile` call, since the data in /proc/ could be
gone any time.

```Haskell
wrap2Maybe :: IO a -> IO (Maybe a)
wrap2Maybe f = catch ((<$>) Just $! f) (\(_ :: IOException) -> return Nothing)

watch args = do
  mStat <- wrap2Maybe (readFile ("/proc/" </> fp </> "stat")) >>=
    return . flip (>>=) parseStat
  print mStat
  watch args
```

But occasionally, my tool exits with message:
GoM: /proc/1484/stat: hGetContents: does not exist (No such process)

Since above code is the only `readFile` call which leads to
hGetContents, I could not figure out what else should I do to catch
the error.
--
竹密岂妨流水过
山高哪阻野云飞


More information about the Haskell-Cafe mailing list