IOError

Dean Herington heringto@cs.unc.edu
Thu, 12 Jun 2003 15:02:17 -0400 (EDT)


On Thu, 12 Jun 2003, Wolfgang Jeltsch wrote:

> On Thursday, 2003-06-12, 18:01, CEST, Filip wrote:
> > Hi,
> >
> > I wrote something like "let t = try (hGetLine h1)" and I would like to check
> > is it EOFError or not. How can I do this ??
> >
> > Thanks
> 
> Hello,
> 
> the above code assigns the I/O action
>     try (hGetLine h1)
> to t. I suppose you want to assign the result of this action to t. If this is 
> the case, you have to write
>     t <- try (hGetLine h1).
> You can then examine t as in the following example:
>     t <- try (hGetLine h1)
>     case t of
>         Left error | isEOFError error
>             -> do <EOF handling>
>         Right result
>             -> do <normal continuation>

Beware: The above causes pattern match failure if a true error (other than 
end of file) occurs.  You probably want something like:

    case t of
        Left error | isEOFError error
            -> do <EOF handling>
                   | otherwise
            -> do <error handling>
        Right result
            -> do <normal continuation>

> A better way might be to use catch which is exported from the Prelude.

Dean